How can I deserialize JSON to a simple Dictionary in ASP.NET?

前端 未结 21 2789
粉色の甜心
粉色の甜心 2020-11-21 06:33

I have a simple key/value list in JSON being sent back to ASP.NET via POST. Example:

{ \"key1\": \"value1\", \"key2\": \"value2\"}

21条回答
  •  心在旅途
    2020-11-21 07:12

    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 object.

    Old code

    private Dictionary deserializeToDictionary(string jo)
    {
        var values = JsonConvert.DeserializeObject>(jo);
        var values2 = new Dictionary();
        foreach (KeyValuePair 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 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 values = deserializeToDictionary(responsestring);
    }
    

    Note: hometown further deserilize into a Dictionary object.

    Update

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

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

    private static object deserializeToDictionaryOrList(string jo,bool isArray=false)
    {
        if (!isArray)
        {
            isArray = jo.Substring(0, 1) == "[";
        }
        if (!isArray)
        {
            var values = JsonConvert.DeserializeObject>(jo);
            var values2 = new Dictionary();
            foreach (KeyValuePair 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>(jo);
            var values2 = new List();
            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;
        }
    }
    
        

    提交回复
    热议问题