1

I have below JSON reply from API.

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

And below is the C# class.

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

But when I am trying below code

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

values in tktStatustemp is null.
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
Ashutosh B Bodake
  • 1,304
  • 1
  • 19
  • 31
  • Your attributes don't match your c# variable names. You can use this to help. https://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net – Sam Marion Aug 23 '17 at 18:52

1 Answers1

7

Change your model to

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

(tested and verified with your json)

L.B
  • 114,136
  • 19
  • 178
  • 224