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

前端 未结 21 2744
粉色の甜心
粉色の甜心 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:10

    Annoyingly enough, if you want to use the default model binders, it looks like you will have to use numerical index values like a form POST.

    See the following excerpt from this article http://msdn.microsoft.com/en-us/magazine/hh781022.aspx:

    Though it’s somewhat counterintuitive, JSON requests have the same requirements—they, too, must adhere to the form post naming syntax. Take, for example, the JSON payload for the previous UnitPrice collection. The pure JSON array syntax for this data would be represented as:

    [ 
      { "Code": "USD", "Amount": 100.00 },
      { "Code": "EUR", "Amount": 73.64 }
    ]
    

    However, the default value providers and model binders require the data to be represented as a JSON form post:

    {
      "UnitPrice[0].Code": "USD",
      "UnitPrice[0].Amount": 100.00,
    
      "UnitPrice[1].Code": "EUR",
      "UnitPrice[1].Amount": 73.64
    }
    

    The complex object collection scenario is perhaps one of the most widely problematic scenarios that developers run into because the syntax isn’t necessarily evident to all developers. However, once you learn the relatively simple syntax for posting complex collections, these scenarios become much easier to deal with.

    0 讨论(0)
  • 2020-11-21 07:11

    Edit: This works, but the accepted answer using Json.NET is much more straightforward. Leaving this one in case someone needs BCL-only code.

    It’s not supported by the .NET framework out of the box. A glaring oversight – not everyone needs to deserialize into objects with named properties. So I ended up rolling my own:

    <Serializable()> Public Class StringStringDictionary
        Implements ISerializable
        Public dict As System.Collections.Generic.Dictionary(Of String, String)
        Public Sub New()
            dict = New System.Collections.Generic.Dictionary(Of String, String)
        End Sub
        Protected Sub New(info As SerializationInfo, _
              context As StreamingContext)
            dict = New System.Collections.Generic.Dictionary(Of String, String)
            For Each entry As SerializationEntry In info
                dict.Add(entry.Name, DirectCast(entry.Value, String))
            Next
        End Sub
        Public Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
            For Each key As String in dict.Keys
                info.AddValue(key, dict.Item(key))
            Next
        End Sub
    End Class
    

    Called with:

    string MyJsonString = "{ \"key1\": \"value1\", \"key2\": \"value2\"}";
    System.Runtime.Serialization.Json.DataContractJsonSerializer dcjs = new
      System.Runtime.Serialization.Json.DataContractJsonSerializer(
        typeof(StringStringDictionary));
    System.IO.MemoryStream ms = new
      System.IO.MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
    StringStringDictionary myfields = (StringStringDictionary)dcjs.ReadObject(ms);
    Response.Write("Value of key2: " + myfields.dict["key2"]);
    

    Sorry for the mix of C# and VB.NET…

    0 讨论(0)
  • 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<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;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:13

    If you're after a lightweight, no-added-references kind of approach, maybe this bit of code I just wrote will work (I can't 100% guarantee robustness though).

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    
    public Dictionary<string, object> ParseJSON(string json)
    {
        int end;
        return ParseJSON(json, 0, out end);
    }
    private Dictionary<string, object> ParseJSON(string json, int start, out int end)
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        bool escbegin = false;
        bool escend = false;
        bool inquotes = false;
        string key = null;
        int cend;
        StringBuilder sb = new StringBuilder();
        Dictionary<string, object> child = null;
        List<object> arraylist = null;
        Regex regex = new Regex(@"\\u([0-9a-z]{4})", RegexOptions.IgnoreCase);
        int autoKey = 0;
        for (int i = start; i < json.Length; i++)
        {
            char c = json[i];
            if (c == '\\') escbegin = !escbegin;
            if (!escbegin)
            {
                if (c == '"')
                {
                    inquotes = !inquotes;
                    if (!inquotes && arraylist != null)
                    {
                        arraylist.Add(DecodeString(regex, sb.ToString()));
                        sb.Length = 0;
                    }
                    continue;
                }
                if (!inquotes)
                {
                    switch (c)
                    {
                        case '{':
                            if (i != start)
                            {
                                child = ParseJSON(json, i, out cend);
                                if (arraylist != null) arraylist.Add(child);
                                else
                                {
                                    dict.Add(key, child);
                                    key = null;
                                }
                                i = cend;
                            }
                            continue;
                        case '}':
                            end = i;
                            if (key != null)
                            {
                                if (arraylist != null) dict.Add(key, arraylist);
                                else dict.Add(key, DecodeString(regex, sb.ToString()));
                            }
                            return dict;
                        case '[':
                            arraylist = new List<object>();
                            continue;
                        case ']':
                            if (key == null)
                            {
                                key = "array" + autoKey.ToString();
                                autoKey++;
                            }
                            if (arraylist != null && sb.Length > 0)
                            {
                                arraylist.Add(sb.ToString());
                                sb.Length = 0;
                            }
                            dict.Add(key, arraylist);
                            arraylist = null;
                            key = null;
                            continue;
                        case ',':
                            if (arraylist == null && key != null)
                            {
                                dict.Add(key, DecodeString(regex, sb.ToString()));
                                key = null;
                                sb.Length = 0;
                            }
                            if (arraylist != null && sb.Length > 0)
                            {
                                arraylist.Add(sb.ToString());
                                sb.Length = 0;
                            }
                           continue;
                        case ':':
                            key = DecodeString(regex, sb.ToString());
                            sb.Length = 0;
                            continue;
                    }
                }
            }
            sb.Append(c);
            if (escend) escbegin = false;
            if (escbegin) escend = true;
            else escend = false;
        }
        end = json.Length - 1;
        return dict; //theoretically shouldn't ever get here
    }
    private string DecodeString(Regex regex, string str)
    {
        return Regex.Unescape(regex.Replace(str, match => char.ConvertFromUtf32(Int32.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.HexNumber))));
    }
    

    [I realise that this violates the OP Limitation #1, but technically, you didn't write it, I did]

    0 讨论(0)
  • 2020-11-21 07:14

    I added a check for null values in the JSON to the other answer

    I had 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.

    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 != null && d.Value.GetType().FullName.Contains("Newtonsoft.Json.Linq.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.

    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 deserialize into a Dictionary<string, object> object.

    0 讨论(0)
  • 2020-11-21 07:19

    Mark Rendle posted this as a comment, I wanted to post it as an answer since it's the only solution that has worked so far to return the success and the error-codes json results from the Google reCaptcha response.

    string jsonReponseString= wClient.DownloadString(requestUrl);    
    IDictionary<string, object> dict = new JavaScriptSerializer().DeserializeObject(jsonReponseString) as IDictionary<string, object>;
    

    Thanks again, Mark!

    0 讨论(0)
提交回复
热议问题