简体   繁体   English

如何使用Newtonsoft.JSON和C#遍历嵌套的JSON字段(并且不使用dynamic关键字)

[英]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 '. 以下JSON文件包含字段“ tileproperties ”。 Within tileproperties there are numbers from 0 to 19. tileproperties中有0到19之间的数字

In this particular case, the amount of numbers are not fixed and the JSON file could contain many more. 在这种特殊情况下,数字的数量是不确定的,JSON文件可能包含更多数量。

Underneath everyone of these numbers there is the field name TileType , with a string value (for example "river01", "river02" etc). 在所有这些数字的下面都有字段名称TileType ,它带有字符串值(例如“ river01”,“ river02”等)。

I would like to read this JSON file and create a dictionary. 我想阅读此JSON文件并创建一个字典。 The key would be the number and the value would be the TileType . 关键是数字,值是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 ) 特别是我想使用Newtonsoft.JSON,C#,并且我无法使用dynamic关键字( 如关于堆栈溢出的类似问题所示

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). 我不确定如何遍历tileproperties字段,因为它似乎没有格式化为数组(如data字段)。

    { "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: 如果只需要tileproperties ,而不需要其余tileproperties ,则可以使用以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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