I have seen many examples that seem to indicate that what I am doing should work, but for some reason this is not the case. I am trying to deserialize a collection of objects, one of the properties of which is a dictionary, for example:
class Program
{
static void Main(string[] args)
{
var json = "{\"Collection\":[{\"ID\":\"1243\",\"Dictionary\":[{\"Key\":\"color\", \"Value\":\"red\"},{\"Key\":\"size\",\"Value\":\"large\"}]},{\"ID\":\"1243\",\"Dictionary\":[{\"Key\":\"color\", \"Value\":\"blue\"},{\"Key\":\"size\",\"Value\":\"small\"}]}]}";
List<MyObject> myObjects = new JavaScriptSerializer().Deserialize<List<MyObject>>(json);
}
}
[DataContract]
public class MyObject
{
[DataMember]
public string ID { get; set; }
[DataMember]
public Dictionary<string, string> Dictionary { get; set; }
}
The first json line encapsulates the whole thing in an object - if I run it, then it works fine, but myObjects is just an empty list. If I run the second line (without wrapping it), I get the following error:
Enter 'System.Collections.Generic.Dictionary`2 [[System.String, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089], [System.String, mscorlib, Version = 4.0.0.0, Culture = neutral , PublicKeyToken = b77a5c561934e089]] 'is not supported for deserializing the array.
, , , : - , JSON , ? JSON , .