0

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?

5
  • Is there any impediment that prevents you from using JSON.NET? Commented Aug 25, 2013 at 7:46
  • I guess not. It appears to be available for Win 8 Apps. I'll investigate that. Any tips on how to accomplish this using JSON.NET? Commented Aug 25, 2013 at 8:10
  • Yes, haha, probably just using json.net your problem will be solved as is, that's the point. Commented Aug 25, 2013 at 17:16
  • @MatíasFidemraizer, JSON.NET on its own hasn't solved the issue. It complains that a Dictionary<TKey, TValue> doesn't implement ICollection<T>, and if I add the JsonArray attribute as suggested, then I end up with the same error as before. Commented Aug 26, 2013 at 7:11
  • Sadly I'm at work and I can't test that. Usually I deserialize objects using the ExpandoObjectConverter and I use a dynamic object. Why don't you try that? Commented Aug 26, 2013 at 10:11

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
    }

Comments

0

Use KeyValuePair class instead of dictionary,

List<KeyValuePair<string, List<string>>> uploadedfiles = JsonConvert.DeserializeObject<List<KeyValuePair<string, List<string>>>>(json)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.