为什么JsonConvert.DeserializeObject返回null值?

时间:2017-08-23 18:47:23

标签: c# json json.net

我有来自API的JSON回复。

{
  "@odata.context": "https://cmcs.crm8.dynamics.com/api/data/v8.2/$metadata#incidents(ticketnumber,statuscode)",
  "value": [{
    "@odata.etag": "W/\"605108\"",
    "ticketnumber": "CAS-00001-Q6C0P8",
    "statuscode": 1,
    "incidentid": "a5d7c7f9-c47d-e711-8123-c4346bdc3c21"
  }, {
    "@odata.etag": "W/\"636397\"",
    "ticketnumber": "CAS-00004-S4C7P3",
    "statuscode": 1,
    "incidentid": "ef3924a5-9c83-e711-8124-c4346bdc3c21"
  }, {
    "@odata.etag": "W/\"633434\"",
    "ticketnumber": "CAS-00009-C5F7J6",
    "statuscode": 1,
    "incidentid": "aa114330-1087-e711-8125-c4346bdc3c21"
  }, {
    "@odata.etag": "W/\"636027\"",
    "ticketnumber": "CAS-00010-L3P5Z1",
    "statuscode": 428350001,
    "incidentid": "4af58898-1f87-e711-8125-c4346bdc3c21"
  }, {
    "@odata.etag": "W/\"606942\"",
    "ticketnumber": "CAS-00002-S9G3Q1",
    "statuscode": 1,
    "incidentid": "99e563d6-4281-e711-8128-c4346bdcdf81"
  }, {
    "@odata.etag": "W/\"636348\"",
    "ticketnumber": "CAS-00003-D7L2W7",
    "statuscode": 1,
    "incidentid": "e5a8dd97-9583-e711-8129-c4346bdcdf81"
  }, {
    "@odata.etag": "W/\"610480\"",
    "ticketnumber": "CAS-00005-Y4J1G7",
    "statuscode": 1,
    "incidentid": "4eb6445c-eb83-e711-8129-c4346bdcdf81"
  }, {
    "@odata.etag": "W/\"636677\"",
    "ticketnumber": "CAS-00006-Y1S9F7",
    "statuscode": 2,
    "incidentid": "81bf1661-ef83-e711-8129-c4346bdcdf81"
  }, {
    "@odata.etag": "W/\"632450\"",
    "ticketnumber": "CAS-00007-M7D4J8",
    "statuscode": 1,
    "incidentid": "e4a38246-ea86-e711-811f-c4346bdd8041"
  }, {
    "@odata.etag": "W/\"633337\"",
    "ticketnumber": "CAS-00008-H8Q9F1",
    "statuscode": 1,
    "incidentid": "c7882927-f186-e711-811f-c4346bdd8041"
  }]
}

下面是C#类。

public class TicketStatus
{
    public string odataContext { get; set; }
    public Value[] values { get; set; }
}

public class Value
{
    public string odata { get; set; }
    public string ticketnumber { get; set; }
    public int statuscode { get; set; }
    public string incidentid { get; set; }
}

但是当我尝试下面的代码时

var strRes = await httpRes.Content.ReadAsStringAsync();
var tktStatustemp = JsonConvert.DeserializeObject<TicketStatus>(strRes);

values in tktStatustemp is null.

1 个答案:

答案 0 :(得分:5)

将模型更改为

public class Value
{
    [JsonProperty("@odata.etag")]
    public string Etag { get; set; }
    public string ticketnumber { get; set; }
    public int statuscode { get; set; }
    public string incidentid { get; set; }
}

public class TicketStatus
{
    [JsonProperty("@odata.context")]
    public string Context { get; set; }
    public List<Value> value { get; set; }
}

(使用您的json测试和验证)