如何将 JSON 数据转换为类对象
JS 代码。
// this is person class constructor.
function personClass(id, name, address, phone) {
this.Id = id;
this.Name = name;
this.Address = address;
this.Phone = phone;
}
var person = new Array();
person.push(new personClass("101", $('#txtName').val(), $('#txtAddress').val(), $('#txtPhone').val());
AjaxRequest = function ("PersonInfo.asmx/AddNewPerson", "{'person[]':'" + JSON.stringify(person) + "'}", successcallback, errorcallback) {
$.ajax({
type: "POST",
url: url,
data: param,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successcallback,
error: errorcallback
});
}
C# 代码。
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod(EnableSession = true)]
public string AddProduct(Person[] person)
{
return person[0].Id;
}
public class Person
{
private string _id = String.Empty;
private string Id
{
get { return _id; }
set { _id = value; }
}
private string _name = String.Empty;
private string Name
{
get { return _name; }
set { _name = value; }
}
private string _address = String.Empty;
private string Address
{
get { return _address; }
set { _address = value; }
}
private string _phone = String.Empty;
private string Id
{
get { return _phone; }
set { _phone = value; }
}
}
问题是:显示以下响应错误消息 -:
{"Message":"无效的网络服务调用, 参数缺失值: \u0027person\u0027.","StackTrace":" 在 System.Web.Script.Services.WebServiceMethodData.CallMethod(对象 目标,IDictionary
2 个参数)\r\n 在 System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象 目标,IDictionary
2 个参数)\r\n 在 System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext 上下文、WebServiceMethodData 方法数据,IDictionary`2 rawParams)\r\n 位于 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文、WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
请提出解决方案。
JS Code.
// this is person class constructor.
function personClass(id, name, address, phone) {
this.Id = id;
this.Name = name;
this.Address = address;
this.Phone = phone;
}
var person = new Array();
person.push(new personClass("101", $('#txtName').val(), $('#txtAddress').val(), $('#txtPhone').val());
AjaxRequest = function ("PersonInfo.asmx/AddNewPerson", "{'person[]':'" + JSON.stringify(person) + "'}", successcallback, errorcallback) {
$.ajax({
type: "POST",
url: url,
data: param,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successcallback,
error: errorcallback
});
}
C# Code.
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod(EnableSession = true)]
public string AddProduct(Person[] person)
{
return person[0].Id;
}
public class Person
{
private string _id = String.Empty;
private string Id
{
get { return _id; }
set { _id = value; }
}
private string _name = String.Empty;
private string Name
{
get { return _name; }
set { _name = value; }
}
private string _address = String.Empty;
private string Address
{
get { return _address; }
set { _address = value; }
}
private string _phone = String.Empty;
private string Id
{
get { return _phone; }
set { _phone = value; }
}
}
Problem is that : show response error message following -:
{"Message":"Invalid web service call,
missing value for parameter:
\u0027person\u0027.","StackTrace":" at
System.Web.Script.Services.WebServiceMethodData.CallMethod(Object
target, IDictionary2 parameters)\r\n
2 parameters)\r\n
at
System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object
target, IDictionary
at
System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext
context, WebServiceMethodData
methodData, IDictionary`2
rawParams)\r\n at
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext
context, WebServiceMethodData
methodData)","ExceptionType":"System.InvalidOperationException"}
Please suggest the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
它与您的代码有两个地方不同:
person
而不是person[]
Try:
It differs from your code in two places:
person
instead ofperson[]