Quantcast
Viewing latest article 18
Browse Latest Browse All 23

Answer by Dasun for How can I deserialize JSON to a simple Dictionary in ASP.NET?

I had the same problem, so I wrote this my self. This solution is differentiated from other answers because it can deserialize in to multiple levels.

Just send JSON string in to deserializeToDictionary function it will return non strongly-typed Dictionary<string, object> object.

Old code

private Dictionary<string, object> deserializeToDictionary(string jo){    var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(jo);    var values2 = new Dictionary<string, object>();    foreach (KeyValuePair<string, object> d in values)    {        // if (d.Value.GetType().FullName.Contains("Newtonsoft.Json.Linq.JObject"))        if (d.Value is JObject)        {            values2.Add(d.Key, deserializeToDictionary(d.Value.ToString()));        }        else        {            values2.Add(d.Key, d.Value);        }    }    return values2;}

Ex: This will return Dictionary<string, object> object of a Facebook JSON response.

Test

private void button1_Click(object sender, EventArgs e){    string responsestring = "{\"id\":\"721055828\",\"name\":\"Dasun Sameera Weerasinghe\",\"first_name\":\"Dasun\",\"middle_name\":\"Sameera\",\"last_name\":\"Weerasinghe\",\"username\":\"dasun\",\"gender\":\"male\",\"locale\":\"en_US\",  hometown: {id: \"108388329191258\", name: \"Moratuwa, Sri Lanka\",}}";    Dictionary<string, object> values = deserializeToDictionary(responsestring);}

Note: hometown further deserilize into a Dictionary<string, object> object.

Update

My old answer works great if there is no array on JSON string. This one further deserialize in to a List<object> if an element is an array.

Just send a JSON string in to deserializeToDictionaryOrList function it will return non strongly-typed Dictionary<string, object> object or List<object>.

private static object deserializeToDictionaryOrList(string jo,bool isArray=false){    if (!isArray)    {        isArray = jo.Substring(0, 1) == "[";    }    if (!isArray)    {        var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(jo);        var values2 = new Dictionary<string, object>();        foreach (KeyValuePair<string, object> d in values)        {            if (d.Value is JObject)            {                values2.Add(d.Key, deserializeToDictionary(d.Value.ToString()));            }            else if (d.Value is JArray)            {                values2.Add(d.Key, deserializeToDictionary(d.Value.ToString(), true));            }            else            {                values2.Add(d.Key, d.Value);            }        }        return values2;    }else    {        var values = JsonConvert.DeserializeObject<List<object>>(jo);        var values2 = new List<object>();        foreach (var d in values)        {            if (d is JObject)            {                values2.Add(deserializeToDictionary(d.ToString()));            }            else if (d is JArray)            {                values2.Add(deserializeToDictionary(d.ToString(), true));            }            else            {                values2.Add(d);            }        }        return values2;    }}

Viewing latest article 18
Browse Latest Browse All 23

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>