在JSON文件中获取元素名称

本文关键字:元素 获取 JSON 文件 | 更新日期: 2023-09-27 17:49:21

我有一个给定的JSON文件:

{"人":{"名称":{"高级":{"名称":"简",
"国家":"我们 " }, " 初级":{"名称":"乔治","国家":"ca"}}}

这是在

中找到的本地状态文件示例之后执行的。

C:'Users'username'Local'Google'Chrome'User Data'Local State

我想要这样的输出:

Senior  
Name: Jan  
Country: US  
Junior  
Name: George  
Country: CA

到目前为止我写的代码:

StringBuilder workLog = new StringBuilder();  
using (StringReader reader = new StringReader(json))
using (JsonReader jsonReader = new JsonTextReader(reader))
 {
 JsonSerializer serializer =  new JsonSerializer();
 var obj = (JToken)serializer.Deserialize(jsonReader);
 //
 var ids = obj["people"]["name"]; 
 foreach (var profile in ids)
  {
  // profile.Name doesn't work, so I guess I need 
  // something here to get the Senior and Junior...
  foreach (var userdata in profile)
   {
   try
    {
     string name = (string)userdata["name"];
     string country=(string)userdata["country"];
     workLog.AppendLine("Profile: " + "[ " + name + " ]" + " country " +   country + Environment.NewLine);
    }
    catch (JsonException je)
    {
     MessageBox.Show(je.Message);
    }
    catch (NullReferenceException nr)
    {
    // todo
    }
   }
  }
}  

如何得到"Senior"answers"Junior"?

更新了jason字符串的开头和结尾。

在JSON文件中获取元素名称

代码

示例在这里运行。

using System;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Program
{
    public static void Main()
    {
        dynamic result = JObject.Parse(json);
        var obj1 = result.people.name;
        foreach (var prop1 in obj1)
        {
            Console.WriteLine(prop1.Name);
            foreach (var obj2 in prop1)
            {
                foreach (var prop2 in obj2)
                {
                    Console.WriteLine(prop2.Name + ": " + prop2.Value);
                }
            }
        }
    }
}

它使用你发布的JSON字符串。

    private static string json = @"

{
    ""people"": {
        ""name"": {
            ""Senior"": {
                ""name"": ""Jan"",
                ""country"": ""US""
            },
            ""Junior"": {
                ""name"": ""George"",
                ""country"": ""CA""
            }
        }
    }
}
    ";
<标题> 输出
Senior
name: Jan
country: US
Junior
name: George
country: CA

您的JSON输入错误,它应该以尖括号开始和结束。

Json有两种主要的容器类型,对象(有字段)和数组(有对象或原语,如int, float, string)。对象以{开始,以}结束,数组以[开始,以]结束。你可以想嵌多少就嵌多少。事物列表应该放在数组中,事物组(就像人的属性)应该放在对象中。

尝试以下JSON格式:

    {
      "people":[
        {
            "name":[ 
                {"Senior":{ "name": "Jan", "country": "US" } },
                {"Junior":{ "name": "George", "country": "CA" } }
            ]
        }
      ]
    }

这将给你一个对象,它有一个数组类型的名为"people"的字段,它包含一个条目,这个条目是一个包含数组类型的字段"name"的对象,它包含2个对象。对象1包含一个名为"Senior"的键,包含字段名和国家;对象2包含一个名为"Junior"的键,包含字段名和国家。

希望这是有意义的,你现在明白JSON是如何工作的。

编辑:OP已经用有效的json编辑了他的帖子,所以,这个答案不再有效。原文为(如后期编辑历史中所见):
string json = "people":{   "name":{ "Senior":{   "name": "Jan",  
"country": "US"   },   "Junior":{   "name": "George",   "country":
"CA"   }   }   }