使用System.Text.Json使用动态键查询或反序列化json [英] Query or deserialize json with dynamic keys using System.Text.Json

查看:204
本文介绍了使用System.Text.Json使用动态键查询或反序列化json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的json,键"123"可以是任何数字.

I have json that looks like this, the key "123" could be any number.

 {
   "key1": "",
   "key2": {
      "items": {
         "123": {
            "pageid": 123,
            "name": "data"
         }
      }
    }
 }

我想使用System.Text.Json反序列化或查询json,以便获得键名称"的值.我该如何使用System.Text.Json?我正在使用.NET Core 3.1.

I want to deserialize or query the json with System.Text.Json so i can get the value of the key "name". How can I do that with System.Text.Json? I'm using .NET Core 3.1.

推荐答案

类似的东西:

public class Rootobject
{
    public string key1 { get; set; }
    public InnerObject key2 { get; set; }
}

public class InnerObject 
{
    public Dictionary<string, ObjectTheThird> items { get; set; }
        = new Dictionary<string, ObjectTheThird>();
}

public class ObjectTheThird
{
    public int pageid { get; set; }
    public string name { get; set; }
}

并使用Dictionary<,>上的API查看项目.或者,您只想要第一个:

and use the APIs on Dictionary<,> to look at the items. Or you just want the first:

var name = obj.key2.items.First().Value.name;

这篇关于使用System.Text.Json使用动态键查询或反序列化json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆