DataContractJsonSerializer help to deserlize Dictionary<string, object[]>


  • I'm trying to use DataContractJsonSerializer to deserialize a JSON string, that looks like:


    {"p1":[123, true], "p2":[456, false]}


    The code that I used to deserialize looks something like:

    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Dictionary<string, object[]>));


    The output for this is always an empty dictionary with no exceptions.


    My question is what should the type parameter be in the constructor? I've noticed that there's another constructor that asks for a knownTypes, could I have perhaps need to put a list of int and bool for what's in the object array? I'm kinda puzzled of what's wrong with this. Please help!



    Tuesday, January 27, 2009 6:14 AM

All replies

  • Dictionaries are serialized in JSONas arrays of KeyValuePair<K, V> - since one can use types other than strings as the key in CLR, it doesn't map directly to a JSON Object.

    To read an arbitrary JSON object into a dictionary using the DataContractJsonSerializer, you can use an ISerializable type:

        public class DeserializingArbitraryJsonObjects  
            static string json = "{\"p1\": [123, true], \"p2\":[456, false]}";  
            public class MyCustomDict : ISerializable  
                public Dictionary<stringobject[]> dict;  
                public MyCustomDict()  
                    dict = new Dictionary<stringobject[]>();  
                protected MyCustomDict(SerializationInfo info, StreamingContext context)  
                    dict = new Dictionary<stringobject[]>();  
                    foreach (var entry in info)  
                        object[] array = entry.Value as object[];  
                        dict.Add(entry.Name, array);  
                public void GetObjectData(SerializationInfo info, StreamingContext context)  
                    foreach (string key in dict.Keys)  
                        info.AddValue(key, dict[key]);  
            static void Main(string[] args)  
                DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(MyCustomDict));  
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));  
                MyCustomDict dict = (MyCustomDict)dcjs.ReadObject(ms);  
    • Proposed as answer by Carlos Figueira Wednesday, January 28, 2009 8:20 PM
    Tuesday, January 27, 2009 6:53 AM
  • This solution doesn't work for Silverlight, since Silverlight doesn't support the Serializable attribute or the ISerializeable interface. Is there a way to read arbitrary JSON in to an IDictionary<string,object> using DataContractJsonSerializer from Silverlight?

    Wednesday, February 22, 2012 8:32 AM
  • Thank you so much Carlos, it really solved my issue.
    Wednesday, June 19, 2013 8:07 AM
  • You are a life saver!  Creating my own serializable class to hold my JSON sub-array is exactly what worked for me.  In my personal case, I made mine a dictionary<string, string> instead--but I have been scouring the Internet for hours looking for a fix and this was the ONLY correct solution I found.  If I could give you 1,000 votes, I would =).
    Friday, November 07, 2014 10:12 PM
  • Hey Carlos....  Create approach.  

    However, seems like it is still missing something for nested data types (i.e p3 value below).

    Have any ideas?

    static string json = "{\"p1\": [123, [432,654]], \"p2\":[456, false], \"p3\":{\"p4\":[432,456]}}";

    • Edited by Go Cats Thursday, September 17, 2015 5:06 PM
    Thursday, September 17, 2015 5:05 PM