使用jquery将复杂对象json发送到Web服务

时间:2013-08-09 13:45:53

标签: jquery web-services asp.net-mvc-4

我正在尝试通过$ .ajax将一个json对象发送到asp.net中的webservice,但是我无法将嵌套对象重新转换为数组。 客户代码:

  var fiscaldocument = {

            "datetime": "2013-08-07 17:37:41", "operatorname": "admin", "total": 21800 
            , "fiscallines":
            [
                { "id": 254, "itemid": "3", "amount": 1000, "price": 2832, "description": "ACQUA", "categoryid": 1, "sequence": 0, "type": 1, "categoryname": "BEVANDE", "taxrate": 21000 },
                { "id": 255, "itemid": "3", "amount": 1000, "price": 5024, "description": "ACQUA", "categoryid": 1, "sequence": 0, "type": 1, "categoryname": "BEVANDE", "taxrate": 21000 },

            ],
            "paymentlines":
            [
                {"paymentform":{"id":1,"name":"creditcard"},"total":1800},
                {"paymentform":{"id":2,"name":"cash"},"total":20000}    
            ]
            };

$.ajax({
        type: "POST",
        url: "webservice.asmx/ExportFiscalDocument",
        data: "{'fiscaldocument':" + JSON.stringify(fiscaldocument) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
        alert("ok");
       }
      });

C#代码代表JSON:

 public class PaymentForm
    {
        int id { get; set; }
        string name { get; set; }

    }

    public class PaymentLine
    {
        public int total { get; set; }
        public PaymentForm paymentform { get; set; }
    }

    public class FiscalLine
    {
        public int id { get; set; }
        public int itemid { get; set; }
        public int amount { get; set; }
        public int price { get; set; }
        public string description { get; set; }
        public int categoryid { get; set; }
        public string type { get; set; }
        public string categoryname { get; set; }
        public int taxrate { get; set; }

    }

    public class FiscalDocument
    {
       public string datetime { get; set; }
       public string operatorname { get; set; }
       public string total { get; set; }
       public List<FiscalLine> fiscallines { get; set; }
       public List<PaymentLine> paymentlines { get; set; }
    }

    [WebMethod]
    public string ExportFiscalDocument(FiscalDocument fiscaldocument)
    {
        return "Hello World";
    }

所有值都可以,但是webservice无法将数据解析为PaymentForm:fiscaldocument.paymentlines [0] .paymentform.id == NULL :-(

enter image description here

如果我以这种方式传递数组值可以:

[{ "id":1,"name":"creditcard","total":1800},
{ "id":1,"name":"cash","total":2000}]

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题

    public class PaymentForm
    {
        int id { get; set; }
        string name { get; set; }
    }

我忘了PUBLIC: - (

    public class PaymentForm
    {
        public int id { get; set; }
        public string name { get; set; }
    }