【问题标题】:Convert a json (with many levels) into a List c#将 json(具有多个级别)转换为 List c#
【发布时间】:2021-08-10 21:58:07
【问题描述】:

我有一个 JSON,里面有很多级别,如下所示

{
    "value": [
        {
            "CityId": 100
            "Colleges": {
                "CollegeName": "ABC",
                "Departments": [
                    {
                        "DepartmentId": 1001,
                        "DepartmentInfo": {
                            "Name": "ComputerScience",
                            "StudentsCount": 60,
                            "HOD": "John"
                        }
                    },
                    {
                        "DepartmentId": 1002,
                        "DepartmentInfo": {
                            "Name": "Humanties",
                            "StudentsCount": 120,
                            "HOD": "Ravi"
                        }
                    }
                ]
            }
        },
        {
            "CityId": 100
            "Colleges": {
                "CollegeName": "DEF",
                "Departments": [
                    {
                        "DepartmentId": 2001,
                        "DepartmentInfo": {
                            "Name": "ComputerScience",
                            "StudentsCount": 60,
                            "HOD": "James"
                        }
                    },
                    {
                        "DepartmentId": 2002,
                        "DepartmentInfo": {
                            "Name": "Humanties",
                            "StudentsCount": 120,
                            "HOD": "Donald"
                        }
                    }
                ]
            }
        }
    ]
}

我获取了 CollegeInfo、DepartmentId、Name、StudentsCount、HOD 等信息。所以我创建了具有这些属性的类。

现在,我可以从下面的代码中获取 Dictionary 中的所有信息。这里的 Key 是 JSON 中属性的路径。

private static void FillDictionaryFromJToken(Dictionary<string, object> dict, JToken token, string path = "")
        {
            switch (token.Type)
            {
                case JTokenType.Object:
                    foreach (JProperty prop in token.Children<JProperty>())
                    {
                        FillDictionaryFromJToken(dict, prop.Value, prop.Path);
                    }
                    break;

                case JTokenType.Array:
                    foreach (JToken value in token.Children())
                    {
                        FillDictionaryFromJToken(dict, value);
                    }
                    break;

                default:
                    dict.Add(path, ((JValue)token).Value);
                    break;
            }
        }

现在第一组的所有属性都是单独的记录,第二组也是如此。路径将是这样的 [0].Colleges.Departments[0].DepartmentId, Colleges.Departments[0].DepartmentInfo.Name 等。 但我想将这些信息作为一条记录填充到类中,其中包含该记录中的所有属性,并且与第二组相同。

有什么帮助吗??

【问题讨论】:

  • 有什么特别的理由不将其解析为类结构(仅使用需要的属性)然后将其映射到所需的吗?
  • 你是在谈论创建这个 json 的确切类结构然后解析它吗?是的,与我必须处理的实际相比,这个 json 包含的属性更少,而且我不需要 json 中的所有属性
  • 然后跳过不需要的属性,但保留层次结构。您可以使用app.quicktype.io 并从生成的类中删除不需要的属性。
  • 我不想用一个类来限制它,因为可能还有其他一些具有不同属性的 jsons
  • 我想说“不限于类”在这种情况下没有给出任何东西,因为你仍然需要了解应该去哪里。

标签: c# json .net generics collections


【解决方案1】:

您可以尝试使用Newtonsoft.Json包,并使用代码:

YourClassWithTheProperties instance = 
    JsonConvert.Deserialize<YourClassWithTheProperties>(jsonString);

这样做您将拥有一个类,并且您可以在属性中使用 foreach。

【讨论】:

  • 为什么不System.Text.Json
  • 嗯,我不知道怎么用,我只用了Newtonsoft.Json。
猜你喜欢
  • 1970-01-01
  • 2017-07-28
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多