0

I'm trying deserialize Json with unknown count iteration. I get menu structure in this format:

[{
    "id": 15,
    "item": "menuTop",
    "child": {
        "id": 16,
        "item": "subMenu",
        "child": {
            "id": 17,
            "item": "Result",
            "child": null
        }
    }
}]:

It's part of my json, so I have to check the last object that says child: null. Otherwise, it may has got more or less number of iteration.

Here is my code. But it doesn't work. I'm not sure if my class item is correct, especially property child

var result = JsonConvert.DeserializeObject<MenuItem>("mySerializedString");

    class MenuItem
    {
        public long id { get; set; }
        public string item { get; set; }
        public List<MenuItem> child { get; set; }
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
tomas
  • 153
  • 2
  • 13
  • 3
    Have you tried just `MenuItem` as the type for `child` instead of `List`? It looks like it refers to a single object, not an array. You probably also want to deserialize to type `>` instead of just `` as it looks like a list instead of a single object. – Kateract Jun 28 '17 at 21:00
  • 3
    Please explain what "id doesn't work" means. What is happening? Are you actually passing the string `"mySerializedString"` – mrfelis Jun 28 '17 at 21:03
  • 1
    If the `child` property is sometimes an array of objects, and sometimes a single object, you can handle this using the converter from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/a/18997172). Also, the root JSON container is an array not an object so need to do `JsonConvert.DeserializeObject>(mySerializedString)` – dbc Jun 28 '17 at 21:09
  • Kateract : Oh yeah, that's true i fix it to simple object and it's work together with dbc advice, change Deserialize Object to List now it's working. Thanks for your advice. Mrfelis : my serialized string i think the json code from my example. I escape it in online editor, but i don't want to put it here once again. – tomas Jun 28 '17 at 21:38
  • @dbc I'm new on StackOverflow and I want to understand the "rules". Is it okay, that Max copied your comment to answer the question? He got the reputation, but I think it should be yours..!? – Sean Stayns Jun 28 '17 at 22:04
  • @SeanStayn I suppose to thank you about the downvote. I don't copy anybody; if you have the correct answer you can post it (and get the reputation...). I cannot delete the CORRECT answer otherwise I would have liked it... – Max Jun 28 '17 at 22:22
  • @SeanStayn if you want to discuss SO policies please do so on meta. Note that searching for existing questions is still required there - https://meta.stackoverflow.com/search?q=answer+in+comments (constructing answer out of comments for on-topic question is always welcome, depending on amount of effort CW may be better). – Alexei Levenkov Jun 28 '17 at 23:17
  • @Max Obviously downvotes on valid answers are not welcome, but they are expected as votes can be cast for any reason (also "quality/usefulness of content" is desired reason for votes). I personally see no need to vote on your answer as it is ok but not exactly adding new info and already essentially provided many times on SO in form of "how to construct classes for JSON using Json2CSharp or similar tool". – Alexei Levenkov Jun 28 '17 at 23:19

1 Answers1

2

You must deserialize as List<MenuItem> and child must be a MenuItem not List

var result = JsonConvert.DeserializeObject<List<MenuItem>>(json);

class MenuItem
{
    public long id { get; set; }
    public string item { get; set; }
    public MenuItem child { get; set; }
}

UPDATE

As @Alexei Levenkov pointed out to me, and thank you, there are some tools that can create C# classes from a json file.

There are web sites where you can paste your json and it generate the C# classes you need to deserialize your json, for example json2charp

At this Stackoverflow question you can find more infos.

Max
  • 6,821
  • 3
  • 43
  • 59