1

我有一个以下 JSON String ,它作为我的 Web-API 的输入参数传递。我在访问多级 JSON 数据时遇到了麻烦,参数中只接收 NULL 值。

{"Customer":{"Abc":67,"Def":"main_user","Hij":"0123","Kel":0},"CustomerOrder":{"OrderID":1,"CartId":1,"Amount":10.00,"LogId":123,"UserId":4},"Actions": [      "value": "New", "onclick": "CreateNewDoc()"},      {"value": "Open", "onclick": OpenDoc()"},      {"value": "Close", "onclick": "CloseDoc()"}    ]}

C#中的类

 public class Rootobject
{
    public Customer Customer { get; set; }
    public Customerorder CustomerOrder { get; set; }
    public Action[] Actions { get; set; }
}

public class Customer
{
    public int Abc { get; set; }
    public string Def { get; set; }
    public string Hij { get; set; }
    public int Kel { get; set; }
}

public class Customerorder
{
    public int OrderID { get; set; }
    public int CartId { get; set; }
    public float Amount { get; set; }
    public int LogId { get; set; }
    public int UserId { get; set; }
}

public class Action
{
    public string value { get; set; }
    public string onclick { get; set; }
}

在这里,我接受来自 postbody 的数据,

[Route("~/FetchData")]
 //   [ActionName("VoucherStatus")]
    [HttpPost]
    //  GET: http://localhost:28056/FetchData/ // 

    public ProcessedResponse<DashBoradController> ProcessVoucherFetch([FromBody] Rootobject  request)
    { // Some operatons } 

是否可以像上面那样读取整个 JSON 字符串?任何人都可以帮助解决,我在下面做了一层 JSON,

{ "Abc":67, "Def":"main_user", "Hij":"0123", "Kel":0 }

但是多级 JSON 给出 NULL。

4

1 回答 1

1

您的 JSON 无效。这是应该绑定到您的对象的有效 JSON:

{
  "Customer": {
    "Abc": 67,
    "Def": "main_user",
    "Hij": "0123",
    "Kel": 0
  },
  "CustomerOrder": {
    "OrderID": 1,
    "CartId": 1,
    "Amount": 10,
    "LogId": 123,
    "UserId": 4
  },
  "Actions": [
    {
      "value": "New",
      "onclick": "CreateNewDoc()"
    },
    {
      "value": "Open",
      "onclick": "OpenDoc()"
    },
    {
      "value": "Close",
      "onclick": "CloseDoc()"
    }
  ]
}
于 2017-04-22T08:37:37.630 回答