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:
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:
var data = JsonHelper.ToClass<RootObject>(JsonString);
And here is the helper class from this article:
Working with JSON in C# & VB[
^]
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;
}
}