I'm trying to use DataContractJsonSerializer to deserialize a JSON string, that looks 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!
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:
- Proposed as answer by Carlos Figueira Wednesday, January 28, 2009 8:20 PM
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?
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 =).