作为参数[FromBody]传递的Json数据是Map to Class,给出NULL

时间:2017-04-22 08:30:55

标签: c# json asp.net-web-api parameter-passing frombodyattribute

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

<?php echo $this->render('_search', ['model' => $searchModel]); ?>
  

C#中的课程

{"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()"}    ]}

我在这里接受来自邮局的数据

 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; }
}

是否可以像上面一样读取整个JSON字符串? 任何人都可以帮忙修复,我已经完成了下面的一层JSON,

{ &#34;美国广播公司&#34;:67, &#34;默认值&#34;:&#34; main_user&#34 ;, &#34; HIJ&#34;:&#34; 0123&#34 ;, &#34;凯尔&#34;:0 }

但是多级JSON给出了NULL。

1 个答案:

答案 0 :(得分: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()"
    }
  ]
}