简体   繁体   中英

How to iterate through a nested JSON field using Newtonsoft.JSON & C# (And without using the dynamic keyword)

The below JSON file contains the field ' tileproperties '. Within tileproperties there are numbers from 0 to 19.

In this particular case, the amount of numbers are not fixed and the JSON file could contain many more.

Underneath everyone of these numbers there is the field name TileType , with a string value (for example "river01", "river02" etc).

I would like to read this JSON file and create a dictionary. The key would be the number and the value would be the TileType . In particular I would like to use Newtonsoft.JSON, C# and I am unable to use the dynamic keyword ( as suggested in a similar question on stack overflow )

I am unsure how to iterate through the tileproperties field as it does not appear to be formatted as an array (like the data field).

    { "height":2,
 "infinite":false,
 "layers":[
        {
         "data":[1, 2, 11, 12, 1, 2, 6, 7, 16, 17, 6, 7],
         "height":2,
         "name":"Tile Layer 1",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":6,
         "x":0,
         "y":0
        }],
 "nextobjectid":1,
 "orientation":"orthogonal",
 "renderorder":"right-up",
 "tiledversion":"1.1.2",
 "tileheight":512,
 "tilesets":[
        {
         "columns":5,
         "firstgid":1,
         "image":"..\/Art\/Sprites\/PrototypeTileSheet.png",
         "imageheight":2048,
         "imagewidth":2560,
         "margin":0,
         "name":"prototypeTiles",
         "spacing":0,
         "tilecount":20,
         "tileheight":512,
         "tileproperties":
            {
             "0":
                {
                 "TileType":"river01"
                },
             "1":
                {
                 "TileType":"river02"
                },
             "10":
                {
                 "TileType":"start01"
                },
             "11":
                {
                 "TileType":"end01"
                },
             "12":
                {
                 "TileType":""
                },
             "13":
                {
                 "TileType":""
                },
             "14":
                {
                 "TileType":""
                },
             "15":
                {
                 "TileType":"tree01"
                },
             "16":
                {
                 "TileType":"tree02"
                },
             "17":
                {
                 "TileType":""
                },
             "18":
                {
                 "TileType":""
                },
             "19":
                {
                 "TileType":""
                },
             "2":
                {
                 "TileType":"unspecified01"
                },
             "3":
                {
                 "TileType":""
                },
             "4":
                {
                 "TileType":""
                },
             "5":
                {
                 "TileType":"grass01"
                },
             "6":
                {
                 "TileType":"grass02"
                },
             "7":
                {
                 "TileType":""
                },
             "8":
                {
                 "TileType":""
                },
             "9":
                {
                 "TileType":""
                }
            },
         "tilepropertytypes":
            {
             "0":
                {
                 "TileType":"string"
                },
             "1":
                {
                 "TileType":"string"
                },
             "10":
                {
                 "TileType":"string"
                },
             "11":
                {
                 "TileType":"string"
                },
             "12":
                {
                 "TileType":"string"
                },
             "13":
                {
                 "TileType":"string"
                },
             "14":
                {
                 "TileType":"string"
                },
             "15":
                {
                 "TileType":"string"
                },
             "16":
                {
                 "TileType":"string"
                },
             "17":
                {
                 "TileType":"string"
                },
             "18":
                {
                 "TileType":"string"
                },
             "19":
                {
                 "TileType":"string"
                },
             "2":
                {
                 "TileType":"string"
                },
             "3":
                {
                 "TileType":"string"
                },
             "4":
                {
                 "TileType":"string"
                },
             "5":
                {
                 "TileType":"string"
                },
             "6":
                {
                 "TileType":"string"
                },
             "7":
                {
                 "TileType":"string"
                },
             "8":
                {
                 "TileType":"string"
                },
             "9":
                {
                 "TileType":"string"
                }
            },
         "tilewidth":512
        }, 
        {
         "firstgid":21,
         "source":"..\/..\/..\/..\/Artsource\/Assets\/Levels\/prototypeTileSet.tsx"
        }],
 "tilewidth":512,
 "type":"map",
 "version":1,
 "width":6
}

If you just need the tileproperties and not the rest, you can use this:

var tileTypes = JObject.Parse(json)
    ["tilesets"][0]["tileproperties"].Children<JProperty>()
    .ToDictionary(x => x.Name, x => x.Value["TileType"].Value<string>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM