How can I parse recursive data structures using JSON.NET?

I have entries MENUITEM

and SCREEN

defined on the root element MENUSTRUCT

from a json string.

What matters is what MENUITEM

others MENUITEM

or SCREEN

s may contain .

I want to parse an entire json string with JSON.NET where I have to get a tree with MENUITEM

which can contain a huge chain of nested records MENUITEM

or SCREEN

.

I have stripped a lot of nested structures from the following json line:

{
"MENUSTRUCT": {
"-text": "GUI.Menu.Root",
"-image": "GUI.Menu.Home",
"-mask": "GUI.Menu.Home.Mask",
"-color": "#E0E0FF",
"-menuid": "MENUTREE",
"MENUITEM": [
  {
    "-text": "GUI.Menu.Text.00000003",
    "-image": "GUI.Menu.Menu",
    "-mask": "GUI.Menu.Menu.Mask",
    "-color": "#C0C0FF",
    "-menuid": "Menu.ID.00000003",
    "SCREEN": [
      {
        "-id": "GUI.Dlg.StartupScreen",
        "-text": "GUI.Menu.Text.00000103",
        "-image": "GUI.Menu.Screen",
        "-mask": "GUI.Menu.Screen.Mask",
        "-menuid": "Menu.ID.00000103"
      },
      {
        "-id": "GUI.Dlg.Calls",
        "-text": "GUI.Menu.Text.Calls",
        "-image": "GUI.Menu.Screen",
        "-mask": "GUI.Menu.Screen.Mask",
        "-menuid": "Menu.ID.00000203"
      }
    ],
    "MENUITEM": [
      {
        "-text": "GUI.Menu.Text.00000603",
        "-image": "GUI.Menu.Menu",
        "-mask": "GUI.Menu.Menu.Mask",
        "-color": "#A0A0FF",
        "-menuid": "Menu.ID.00000603",
        "SCREEN": [
          {
            "-id": "GUI.Dlg.SpecialTrips",
            "-text": "GUI.Menu.Text.00010603",
            "-image": "GUI.Menu.Screen",
            "-mask": "GUI.Menu.Screen.Mask",
            "-menuid": "Menu.ID.00010603"
          },
          {
            "-id": "GUI.Dlg.SpecialTrips",
            "-text": "GUI.Menu.Text.00020603",
            "-image": "GUI.Menu.Screen",
            "-mask": "GUI.Menu.Screen.Mask",
            "-menuid": "Menu.ID.00020603"
          }
         ]  // end of screen
        }
     ]     // end of nested menuitem
  },
      /* snipped following menuitems */
]  // end of menuitem array
}
}

      

I tried to deserialize the whole thing with

JsonConvert.DeserializeObject<List<MenuItem>>(jsonString);

      

I have an object MENUITEM

that has a property List<Screen>

and one of List<MenuItem>

, but none of this works. I think the deserializer is not capable of handling nested MENUITEM

s.

I also tried to JObject

parse the tree using , but I think so you need to implement a lot of parsing.

JObject obj = JObject.Parse(json);   
var menuItems = from m in obj["MENUSTRUCT"]["MENUITEM"].Children() select m;
List<MenuEntry> menues = this.LoadMenuStructure(menuItems);

      

Here I have to make sure I am in the correct one JToken

, but I have no idea if I am processing with MENUITEM

or SCREEN

.

Isn't there another, easier way to deserialize a json string without sticking me through all the structures?

Thanks in advance for your help.

+3


source to share


1 answer


Your original Script has errors in it and it has an extra space ,

at the bottom. I removed the C # //

Comment tags and now this should work for you.

{
    "MENUSTRUCT": {
        "-text": "GUI.Menu.Root",
        "-image": "GUI.Menu.Home",
        "-mask": "GUI.Menu.Home.Mask",
        "-color": "#E0E0FF",
        "-menuid": "MENUTREE",
        "MENUITEM": [
            {
                "-text": "GUI.Menu.Text.00000003",
                "-image": "GUI.Menu.Menu",
                "-mask": "GUI.Menu.Menu.Mask",
                "-color": "#C0C0FF",
                "-menuid": "Menu.ID.00000003",
                "SCREEN": [
                    {
                        "-id": "GUI.Dlg.StartupScreen",
                        "-text": "GUI.Menu.Text.00000103",
                        "-image": "GUI.Menu.Screen",
                        "-mask": "GUI.Menu.Screen.Mask",
                        "-menuid": "Menu.ID.00000103"
                    },
                    {
                        "-id": "GUI.Dlg.Calls",
                        "-text": "GUI.Menu.Text.Calls",
                        "-image": "GUI.Menu.Screen",
                        "-mask": "GUI.Menu.Screen.Mask",
                        "-menuid": "Menu.ID.00000203"
                    }
                ],
                "MENUITEM": [
                    {
                        "-text": "GUI.Menu.Text.00000603",
                        "-image": "GUI.Menu.Menu",
                        "-mask": "GUI.Menu.Menu.Mask",
                        "-color": "#A0A0FF",
                        "-menuid": "Menu.ID.00000603",
                        "SCREEN": [
                            {
                                "-id": "GUI.Dlg.SpecialTrips",
                                "-text": "GUI.Menu.Text.00010603",
                                "-image": "GUI.Menu.Screen",
                                "-mask": "GUI.Menu.Screen.Mask",
                                "-menuid": "Menu.ID.00010603"
                            },
                            {
                                "-id": "GUI.Dlg.SpecialTrips",
                                "-text": "GUI.Menu.Text.00020603",
                                "-image": "GUI.Menu.Screen",
                                "-mask": "GUI.Menu.Screen.Mask",
                                "-menuid": "Menu.ID.00020603"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

      



JSONLint JSON Validator

+2


source







All Articles