我正在尝试通过 $.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");
}
});
JSON的C#代码表示:
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";
}
所有值都可以,但 Web 服务无法将数据解析为 PaymentForm:finishdocument.paymentlines[0].paymentform.id == NULL :-(
如果我以这种方式传递数组值是可以的:
[{ "id":1,"name":"creditcard","total":1800},
{ "id":1,"name":"cash","total":2000}]