【问题标题】:How to iterate through a nested JSON field using Newtonsoft.JSON & C# (And without using the dynamic keyword)如何使用 Newtonsoft.JSON 和 C# 遍历嵌套的 JSON 字段(并且不使用动态关键字)
【发布时间】:2018-02-07 17:30:33
【问题描述】:

以下 JSON 文件包含字段“tileproperties”。在 tileproperties 中有 numbers 从 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
}

【问题讨论】:

  • 这是stackoverflow.com/q/48582064/173225 的扩展,不是吗?你能修改你的JSON吗? { "key": "0", "TileType": "river01" }, ... 会更容易使用。为什么你不能使用dynamic 并且必须使用 Newtonsoft?否则@Xiaoy312 的答案是最简单的解决方案。

标签: c# json unity3d


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2020-05-20
    • 2019-01-14
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多