简体   繁体   English

从嵌套的 json 对象中获取键名

[英]Get Key Name from nested json object

I am new to C# and JSON and need some help in getting the Key name(s) in a list of a nested JSON object.我是 C# 和 JSON 的新手,需要一些帮助来获取嵌套 JSON 对象列表中的键名。 The keys are dynamic so I won't necessarily know the keys.键是动态的,所以我不一定知道键。

sample code I've tried.我试过的示例代码。

```
protected void test()
{
        var mystring = @"{
          ""zone1"": {
            ""sites"": {
              ""site1"": {
                ""to"": ""email1"", 
                ""subject"": ""subjecttxt"", 
                ""link"": ""somesite""
            },
            ""site2"": {
              ""to"": ""email1"", 
              ""subject"": ""subject"",
              ""link"": ""somesite""
            }
          }, 
          ""zone2"": {
            ""to"": ""email1"", 
            ""subject"": ""subject"", 
            ""link"": ""somelink""
          }}";
        var rss = JObject.Parse(mystring);

        foreach (var section in rss)
        {
            Console.Write(section.Key);
            IList<JToken> result = rss["zone1"]["sites"].Children().ToList();
            var zone = section.Key;
            var site = rss[zone]["sites"];
            foreach (var subsite in rss["zone1"]["sites"])
            {
                var subs = subsite.Parent.ToString();
                // some other code
            }
         }
}
```

Looking for a result:寻找结果:

site1, site2, ...站点 1、站点 2、...

I can get the children as IList but looking for something similar to "section.Key" as noted above.我可以将孩子作为 IList 但寻找类似于上面提到的“section.Key”的东西。

Thank you for your help.感谢您的帮助。

I believe what you are looking for is to get the properties of the sites.我相信您正在寻找的是获取站点的属性。 Since accessing the rss["zone1"]["sites"] returns a JToken , you will need to convert that to JObject and then use Properties() method to get the data you need.由于访问rss["zone1"]["sites"]返回一个JToken ,您需要将其转换为JObject ,然后使用 Properties() 方法来获取您需要的数据。

var sites = ((JObject)rss["zone1"]["sites"]).Properties();

Then you can simply iterate over the IEnumerable<Jproperty> to get the Name of the property or whatever else you need from under it.然后,您可以简单地遍历IEnumerable<Jproperty>以获取属性的Name或您需要的任何其他内容。

To get the section.Key for the sites, you can use the following code.要获取站点的 section.Key,您可以使用以下代码。

foreach(var site in (JObject)rss["zone1"]["sites"]) {
    Console.WriteLine(site.Key);
}

Output:输出:

site1
site2

Your first call to JObject.Parse already does all the work of converting a string into a structured JSON object.您对JObject.Parse第一次调用已经完成了将字符串转换为结构化 JSON 对象的所有工作。 The currently-accepted answer redoes some of this work by (1) turning a structured JSON object back into a string, and then (2) re-parsing it with JObject.Parse .当前接受的答案通过 (1) 将结构化 JSON 对象重新转换为字符串,然后 (2) 使用JObject.Parse重新解析它来JObject.Parse There is a simpler way.有一个更简单的方法。

Instead, you can cast the value stored at rss["zone1"]["sites"] into a JObject .相反,您可以将rss["zone1"]["sites"]存储的值转换为JObject (The expression rss["zone1"]["sites"] has type JToken , which is a parent class of JObject , but in this case we happen to know that rss["zone1"]["sites"] is always JSON object, ie a collection of key-value pairs. Therefore, this cast is safe to perform.) (表达rss["zone1"]["sites"]的类型为JToken ,这是一个父类的JObject ,但在这种情况下,我们碰巧知道rss["zone1"]["sites"]总是JSON对象,即键值对的集合。因此,执行此转换是安全的。)

This is what the code might look like:代码可能如下所示:

var sites = (JObject) rss["zone1"]["sites"];
foreach (var site in sites)
{
  Console.WriteLine(site.Key);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM