Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

What is the proper way to deserialize this JSON string? It is simply an array of dictionaries where each dict has a "title" and "children" where children is another array of dicts.

I am using this as a TreeView item source, but the treeview only displays the Title1 > Child1 because I assume something is wrong with the deserializing I'm doing. I also try to print out Child1's first child but can't figure out how to do it. The code below has an invalid cast exception.

s = @"[{""title"":""Title1"",""children"":[{""title"":""Child1"",""children"":[{""title"":""grandchild1"",""children"":[{""title"":""Huh""}]}] }] }]";
List<Dictionary<string, object>> marr = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(s);
mTreeView.ItemsSource = marr;
List<Dictionary<string,object>> cs = (List<Dictionary<string,object>>)marr[0]["children"];
Debug.WriteLine(cs[0]["title"]);
                I don't see why you want to create classes out of a JSON string. I don't plan on breaking my code simply because the JSON string format changes.
– user317033
                Apr 9, 2013 at 17:57
  • Try as I might, I don't see a "dictionary" here so much as a recursive list of the object above, and
  • This is the actual definition of the object your want out anyway, so you can take advantage of all the benefits of having a real type, rather than just a dictionary of strings.
  • Note to address your comments: If the JSON string changes, it will not break your code; extraneous properties will be ignored and missing properties will correctly get set to null.

    {""title"":""Title1""} is a dictionary with key title value title1. JSON is a specification that only does arrays and dictionaries. – user317033 Apr 9, 2013 at 11:22 If JSON.net doesn't support JSON correctly ie: [] List and {} dictionary does anyone know of an alternative that does? – user317033 Apr 9, 2013 at 11:35 I've edited my answer to clarify a little bit and address your concerns. Frankly though, you haven't really given enough information to say why you don't want to use a class. – lc. Apr 10, 2013 at 2:24 This is a really old answer & comment but not wanting to use a class is an entirely valid reason. Hopefully c#7 will actually deliver direct json support as a builtin type instead of these psuedo-approximations everything else is – Chris Marisic May 13, 2016 at 14:04

    https://codetitans.codeplex.com/

    codetitans JSON supports correct parsing of JSON into array/dict of primitives as follows:

    JSonReader jr = new JSonReader();
    IJSonObject json = jr.ReadAsJSonObject(s);
    Debug.WriteLine(json[0]["children"][0]["title"]);
    

    As far as I can tell it is the only C# library that does.

    It looks like you can do this with JSON.NET out of box currently

    var @object = JsonConvert.DeserializeObject(s)
    var slightlyMoreUsefulObject = (JArray)@object;
    var actualObject = slightlyMoreUsefulObject[0]
    var topLevelTitle = actualObject["title"]
    var children = actualObject["children"]
    var firstChild = children[0]
    var firstChildTitle = firstChild["title"]
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.