Reputation: 12814
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?
Upvotes: 0
Views: 10607
Reputation: 369
Use KeyValuePair class instead of dictionary,
List<KeyValuePair<string, List<string>>> uploadedfiles =
JsonConvert.DeserializeObject<List<KeyValuePair<string, List<string>>>>(json)
Upvotes: 0
Reputation: 1866
Having: C#
public class wrap
{
public List<Dictionary<string, List<string>>> folders { get; set; }
}
Json:
{"folders":[
{"Videos":[1196717,2874999,898084]},
{"Fun":[2443301,3671]},
{"News":[62689,58867,11385]}
]
}
Using NewtonJson deserializer:
JsonDeserializer.Deserialize<wrap>(JsonInput)
I got successful object with List of 3 items of dictionaries.
Using DataContractJsonSerializer:
var serialzier = new DataContractJsonSerializer(typeof(wrap));
var stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonInput));
Result = (wrap)serialzier.ReadObject(stream);
I got Unsuccessful object with List of 3 items of dictionaries, but they were empty. Maybe DataContractJsonSerializer needs some tweaking?
Hope it helps.
Upvotes: 1
Reputation: 2095
I used JavaScriptSerializer
for this. Basically your json is actually a Dictionary<string, object>
. I'm not sure why it would not let to do the casting to list, but any way, here are few simple lines which allows you to accomplish your task.
Note that i used this as jsonString (i.e without the "video:" part)
[
{"Videos":[1196717,2874999,898084]},
{"Fun":[2443301,3671]},
{"News":[62689,58867,11385]}
]
If you want it with the video, then it is one more level of dictionary, i.e. Dictionary<string, Dictionary<string, object>>
but works in same manner
Code:
Dictionary<string, List<int>> result = new Dictionary<string, List<int>>();
object[] desirializedJsonObject =(object[])new JavaScriptSerializer().DeserializeObject(jsonString);
foreach (var obj in desirializedJsonObject)
{
// Get the record
var firstRecord = ((Dictionary<string, object>)obj).First();
// Creat list of values
var listOfValues = ((object[])firstRecord.Value).Select(x => Convert.ToInt32(x)).ToList();
result.Add(firstRecord.Key, listOfValues);
}
Upvotes: 0