I have a JSON node similar to the following.
"folders":[
{"Videos":[1196717,2874999,898084]},
{"Fun":[2443301,3671]},
{"News":[62689,58867,11385]}
]
I want to deserialize it into a Dictionary<string, List<int>> or something similar. I currently have the member:
[DataMember(Name = "folders")]
public Dictionary<string, List<int>> Folders;
And I expect an output like:
Folders = new Dictionary<string, List<int>>() {
{"Videos", new List<int>() { 1196717, 2874999, 898084 }},
{"Fun", new List<int>() { 2443301, 3671 }},
{"News", new List<int>() { 62689, 58867, 11385 }}
};
I've implemented the deserializer as:
var serializer = new DataContractJsonSerializer(
typeof(T),
new DataContractJsonSerializerSettings() {
DateTimeFormat = new DateTimeFormat("yyyy-MM-ddTHH:mm:ss.fffffffZ"),
}
);
T result = (T)serializer.ReadObject(response);
But the just produces the error:
The data contract type 'System.Runtime.Serialization.KeyValue`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' cannot be deserialized because the required data members 'Key, Value' were not found.
I understand this is because it is expecting something more like this, but the format is out of my control.
"folders":[
{
"key":"Videos",
"value":[1196717,2874999,898084]
},{
"key":"Fun",
"value":[2443301,3671]
},{
"key":"News",
"value":[62689,58867,11385]
}
]
What can I do to deserialize this?
Dictionary<TKey, TValue>doesn't implementICollection<T>, and if I add theJsonArrayattribute as suggested, then I end up with the same error as before.ExpandoObjectConverterand I use a dynamic object. Why don't you try that?