Cannot Deserialize Object Json Rest API

Hi Everyone
Cannot Deserialize to the Object
Thank In Advance

This is my Json String
`

[{"$id":"1",
"Id":1,
"Title":"Title Name",
"Remark":"Remark Text",
"Code":"HIGH",
"StatCode":"Status Code",
"StartDate":"2017-10-25T15:55:00",
"DueDate":"2017-11-23T11:35:00",
"EstimatedTime":"0 Year(s) | 0 Month(s) | 28 Day(s) | 19 Hour(s) | 40 Minute(s)"}]`

and this is the Class

public class Deatail { public int Id { get; set; } public string Title { get; set; } public string Remark { get; set; } public string Code { get; set; } public string StatCode { get; set; } public DateTime StartDate { get; set; } public DateTime DueDate { get; set; } public string EstimatedTime { get; set; } }

and this is the code

`
string RestUrl = "My Link Here";

        client.DefaultRequestHeaders.Accept.Add(
            new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        var uri = new Uri(string.Format(RestUrl, string.Empty));

        var response = client.GetAsync(uri).Result;
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            var Data = JsonConvert.DeserializeObject<WoDeatail>(content);
        }

`

Tagged:

Answers

  • Gigex42Gigex42 USMember ✭✭✭✭

    Try using json2csharp to convert your json to a csclass.

    public class RootObject
    {
        public string __invalid_name__$id { get; set; }
        public int Id { get; set; }
        public string Title { get; set; }
        public string Remark { get; set; }
        public string Code { get; set; }
        public string StatCode { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime DueDate { get; set; }
        public string EstimatedTime { get; set; }
    }
    

    This is the result. Seems like the first string property has some problem?
    Also please write your error message and where you got the error in your code.

    Also shouldnt it be public class Detail instead of Deatail :wink::smiley:

  • ManojChamikaraManojChamikara USMember ✭✭

    @Gigex42 said:
    Try using json2csharp to convert your json to a csclass.

    public class RootObject
    {
        public string __invalid_name__$id { get; set; }
        public int Id { get; set; }
        public string Title { get; set; }
        public string Remark { get; set; }
        public string Code { get; set; }
        public string StatCode { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime DueDate { get; set; }
        public string EstimatedTime { get; set; }
    }
    

    This is the result. Seems like the first string property has some problem?
    Also please write your error message and where you got the error in your code.

    Also shouldnt it be public class Detail instead of Deatail :wink::smiley:

    Thank for the Fast reply
    when executing this line jump in to next line but var is null
    var Data = JsonConvert.DeserializeObject(content);

  • Try
    var Data=JsonConvert.DeserializeObject<List(content);

Sign In or Register to comment.