反序列化Json字典以列出

我有一个 Json 文件,对象序列化正确,但问题是 json 看起来像一个字典,键是字符串“0”、“1”等等。


有没有什么方法可以在不涉及编写自己的解析器的情况下将它们正确反序列化为列表?


"WeaponSlots":{

        "0":{

           "WeaponInstalled":null,

           "AllowedWeaponTypes":{

              "0":{

                 "0":2

              }

           },

           "AllowedWeapons":null

        },

        "1":{

           "WeaponInstalled":null,

           "AllowedWeaponTypes":{

              "0":{

                 "0":2

              }

           },

           "AllowedWeapons":null

        }

示例文件:https : //pastebin.com/i3LQ3L7j


陪伴而非守候
浏览 142回答 2
2回答

慕哥9229398

您可以使用数据类型Dictionary<string, object>来反序列化这个..

jeck猫

static void Main(string[] args){&nbsp; &nbsp; // load the file.&nbsp; &nbsp; var file = File.ReadAllText("Example.json");&nbsp; &nbsp; // to generate the 'Example' classes from JSON I used&nbsp; &nbsp; // https://app.quicktype.io and changed the name to 'Example'&nbsp; &nbsp; var example = JsonConvert.DeserializeObject<Example>(file);&nbsp; &nbsp; // select the value of each dictionary entry into a list.&nbsp; &nbsp; var sections = example.Sections.Select(x => x.Value).ToList();}
打开App,查看更多内容
随时随地看视频慕课网APP