为了账号安全,请及时绑定邮箱和手机立即绑定

反序列化Json字典以列出

反序列化Json字典以列出

C#
陪伴而非守候 2022-01-09 15:08:13
我有一个 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
查看完整描述

2 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

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


查看完整回答
反对 回复 2022-01-09
?
jeck猫

TA贡献1909条经验 获得超7个赞

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();

}


查看完整回答
反对 回复 2022-01-09
  • 2 回答
  • 0 关注
  • 141 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号