json 使用 POST 方法从浏览器传递到服务器中的 ASP.NET MVC4 应用程序控制器。它包含属性,其中 3 个是 0.. 20 个元素的数组(在下面的代码中,它们都只有 1 个元素)。如何在 C# 中解析这个 json?我尝试了带有签名的控制器
public JsonResult RegisterSales(Sale mysale)
但未分配 mysale 属性。
通过json:
{ "id":"sale1",
"sale_date":"2013-11-10 19:20:44"
"taxes":[{"id":"km20pr","name":"20%","rate":0.2}],
"products":[{"id":"prod1",
"sale_id":"sale1",
"register_id":"register1",
"quantity":"1.00000"}],
"payments":[{"id":"payment1",
"sale_id":"sale1",
"register_id":"register1",
"amount": 0
}]
}
它应该解析为 C# 结构,例如
public class Sale
{
public string id;
public DateTime sale_date;
public Tax[] taxes;
public Product[] products;
public Payment[] payments;
}
public class Tax
{
public string id, name;
public decimal rate;
}
public class Product
{
public string id, sale_id, register_id;
public decimal quantity;
}
public class Payment
{
public string id, sale_id, register_id;
public decimal amount;
}