反序列化Json字典以列出[重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

我有一个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

c# .net json json.net
2个回答
0
投票

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


0
投票
static void Main(string[] args)
{
    // load the file.
    var file = File.ReadAllText("Example.json");

    // to generate the 'Example' classes from JSON I used
    // https://app.quicktype.io and changed the name to 'Example'
    var example = JsonConvert.DeserializeObject<Example>(file);

    // select the value of each dictionary entry into a list.
    var sections = example.Sections.Select(x => x.Value).ToList();
}
© www.soinside.com 2019 - 2024. All rights reserved.