Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can you please assist how to deserialise the below Json string?

string JsonString = @"{'api_res_key':'TestKey','res_status':'200','res_data':{'32':{ 'state_id':'32','state_name':'ANDAMAN & NICOBAR'},'1':{ 'state_id':'1','state_name':'ANDHRA PRADESH'},'3':{ 'state_id':'3','state_name':'ARUNACHAL PRADESH'}}}";
            JObject person = JObject.Parse(JsonString);

What I have tried:

I had tried to deserialise in following way but it gives me exception...


<pre>    public class RootObject
    {
        public string api_res_key { get; set; }
        public string res_status { get; set; }
        public ResData[] res_data { get; set; }
    }

    public class ResData
    {
        public string state_id { get; set; }
        public string state_name { get; set; }
    }


var Data = JsonConvert.DeserializeObject<RootObject[]>(JsonString);
Posted
Updated 29-Jun-18 11:36am

1 solution

The reason that you're having problems is that you can't deserialize a Dictionary into a List.

When you look at the JSON 'res_data' object, there are 3 sets of key & value pairs:
'32':{ 'state_id':'32','state_name':'ANDAMAN & NICOBAR'},
'1' :{ 'state_id':'1','state_name':'ANDHRA PRADESH'},
'3' :{ 'state_id':'3','state_name':'ARUNACHAL PRADESH'}


So to fix, You need to do something like:
C#
public class State
{
    [JsonProperty("state_id")]
    public string StateId { get; set; }

    [JsonProperty("state_name")]
    public string StateName { get; set; }
}

public class RootObject
{
    [JsonProperty("api_res_key")]
    public string ApiResKey { get; set; }

    [JsonProperty("res_status")]
    public string ResStatus { get; set; }

    [JsonProperty("res_data")]
    public Dictionary<int, State> ResData { get; set; }
}

Then you can deserialize the JSON:
C#
var data = JsonHelper.ToClass<RootObject>(JsonString);

And here is the helper class from this article: Working with JSON in C# & VB[^]
C#
public static class JsonHelper
{
    public static string FromClass<T>(T data, bool isEmptyToNull = false,
                                        JsonSerializerSettings jsonSettings = null)
    {
        string response = string.Empty;

        if (!EqualityComparer<T>.Default.Equals(data, default(T)))
            response = JsonConvert.SerializeObject(data, jsonSettings);

        return isEmptyToNull ? (response == "{}" ? "null" : response) : response;
    }

    public static T ToClass<T>(string data, JsonSerializerSettings jsonSettings = null)
    {
        var response = default(T);

        if (!string.IsNullOrEmpty(data))
            response = jsonSettings == null
                ? JsonConvert.DeserializeObject<T>(data)
                : JsonConvert.DeserializeObject<T>(data, jsonSettings);

        return response;
    }
}
 
Share this answer
 
Comments
Ajay_Saini 30-Jun-18 12:58pm    
Thank you so much.... it works as expected. What to do if res_data is of Array type.

string JsonString = @"{'api_res_key':'HASH777VALID','res_status':'200','res_data':[{ 'state_id':'32','state_name':'ANDAMAN & NICOBAR'},{ 'state_id':'1','state_name':'ANDHRA PRADESH'},{ 'state_id':'3','state_name':'ARUNACHAL PRADESH'}]}";

What change i need to do to deserialize it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900