Home > Back-end >  Null Object-DeSerializing JSON C#
Null Object-DeSerializing JSON C#

Time:12-15

Returned object :

{
    "_expands": [],
    "size": 3,
    "start": 3,
    "limit": 3,
    "isLastPage": false,
    "_links": {
        "base": "http://host:port/context/rest/desk",
        "context": "context",
        "next": "http://host:port/context/rest/desk",
        "prev": "http://host:port/context/rest/desk"
    },
    "values": [
        {
            "status": "Waiting for Customer",
            "statusDate": {
                "iso8601": "2015-10-08T14:05:00 0700",
                "polaris": "2015-10-08T14:05:00.000 0700",
                "friendly": "Today 14:05 PM",
                "epochMillis": 1444287900000
            }
        },
        {
            "status": "Waiting for Support",
            "statusDate": {
                "iso8601": "2015-10-08T14:01:00 0700",
                "polaris": "2015-10-08T14:01:00.000 0700",
                "friendly": "Today 14:01 PM",
                "epochMillis": 1444287660000
            }
        },
        {
            "status": "Waiting for Customer",
            "statusDate": {
                "iso8601": "2015-10-08T14:00:00 0700",
                "polaris": "2015-10-08T14:00:00.000 0700",
                "friendly": "Today 14:00 PM",
                "epochMillis": 1444287600000
            }
        }
    ]
}

Classes :

 public class polarisState
{
    public string[] expands { get; set; }
    public int size { get; set; }
    public int start { get; set; }
    public int limit { get; set; }
    public bool isLastPage { get; set; }
    public _links links { get; set; }
    public values[] values { get; set; }
}




public class _links
{

    //public string base {get; set;}
    public string context { get; set; }
    public string next { get; set; }
    public string prev { get; set; }
}

public class values
{

    public string status { get; set; }
    public statusDate statusDate { get; set; }

}

public class statusDate
{

    public string iso8601 { get; set; }
    public string polaris { get; set; }
    public string friendly { get; set; }
    public int epochMillis { get; set; }

}

code below :

 if (resp2.IsSuccessStatusCode)
    {
       

        var values = JsonConvert.DeserializeObject<JiraState>(resp2.Content.ReadAsStringAsync().Result); }

CodePudding user response:

You should change the class polarisState to

public class polarisState
{
    public List<string> expands { get; set; }
    public int size { get; set; }
    public int start { get; set; }
    public int limit { get; set; }
    public bool isLastPage { get; set; }
    public _links links { get; set; }
    public List<values> values { get; set; }
}

array to List. Then change the statusDate to:

public class statusDate
{

    public string iso8601 { get; set; }
    public string polaris { get; set; }
    public string friendly { get; set; }
    public long epochMillis { get; set; }

}

change the epochMillis from int to long value type. It's done.

Use the async method and change the code as follow:

var content = await resp2.Content.ReadAsStringAsync();
var values = JsonConvert.DeserializeObject<polarisState>(content);
  • Related