小编Bee*_*ion的帖子

C# 迭代 JSON 对象

我正在尝试使用 .Net Json 反序列化器System.Text.Json来迭代以下 Json 输入。

如果“person_id”是 json 结构中的顶层(见下文),我找到了按“person_id”进行迭代的方法,但我无法找到循环遍历所有下一级项目的方法。

我认为 NewtonSoftJArrayJObject非常接近我所需要的(我可能会追求这一点),但不确定微软的解决方案是否有办法做到这一点......

System.Text.Json微软的库可以做到这一点吗?

{
  "0": {
    "person_id": "0",
    "last_name": "Greg",
    "first_name": "Craig",
  },
  "1": {
    "person_id": "1",
    "last_name": "Doe",
    "first_name": "John",
  }
}
Run Code Online (Sandbox Code Playgroud)

JsonElement通过属性名称提取对象的解决方案(即我可以通过这种方式获取 John Doe 的信息)。

using (JsonDocument document = JsonDocument.Parse(jsonString))
{
    JsonElement root = document.RootElement;

    JsonElement studentsElement = root.GetProperty("1");
}
Run Code Online (Sandbox Code Playgroud)

c# json .net-core

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

.net-core ×1

c# ×1

json ×1