如何使用Newtonsoft.JSON&amp ;;迭代嵌套的JSON字段C#(并且不使用动态关键字)

时间:2018-02-07 17:30:33

标签: c# json unity3d

以下JSON文件包含“ tileproperties ”字段。在 tileproperties 中,数字从0到19.

在这种特殊情况下,数量不固定,JSON文件可能包含更多。

在这些数字的每个下方都有字段名称 TileType ,带有字符串值(例如“river01”,“river02”等)。

我想阅读这个JSON文件并创建一个字典。关键是数字,值为 TileType 。特别是我想使用Newtonsoft.JSON,C#,我无法使用动态关键字(as suggested in a similar question on stack overflow

我不确定如何遍历 tileproperties 字段,因为它似乎没有被格式化为数组(如数据字段)。

    { "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
}

1 个答案:

答案 0 :(得分:5)

如果你只需要tileproperties而不需要其余的,你可以使用:

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