如何将 JSON 数据转换为类对象

发布于 2024-10-27 23:46:55 字数 2174 浏览 1 评论 0原文

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(对象 目标,IDictionary2 个参数)\r\n 在 System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象 目标,IDictionary2 个参数)\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
at
System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object
target, IDictionary
2 parameters)\r\n
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2024-11-03 23:46:55

尝试:

"{'person':" + JSON.stringify(person) + "}"

它与您的代码有两个地方不同:

  1. person 而不是 person[]
  2. person 对象周围没有引号

Try:

"{'person':" + JSON.stringify(person) + "}"

It differs from your code in two places:

  1. person instead of person[]
  2. No quotes around person object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文