gpt4 book ai didi

c# - 如何使用 SimpleJson c# 获取 Json 文件中的 key

转载 作者:行者123 更新时间:2023-12-02 00:45:58 24 4
gpt4 key购买 nike

我有一个这种格式的 json 文件:

{
"3874D632": {
"FirstName": "Jack",
"LastName": "Andersen"
},
"34F43A33": {
"FirstName": "John",
"LastName": "Smith"
}

StreamReader read_json = new StreamReader(path_json_file);

json = JSONNode.Parse(read_json.ReadToEnd());
read_json.Close();

如何获取3874D632或34F43A33 ??

json[????].

最佳答案

Note: this answer refers to the SimpleJSON library posted on the UnifyWiki site, which is not to be confused with the completely different SimpleJson library that ships as part of RestSharp.

如果 JSONNode 表示一个 JSON 对象,您可以将其转换为 JSONObject,然后您可以从那里像字典一样枚举键值对。每个键值对的 Key 将具有您要查找的值。请参见下面的示例:

string json = @"
{
""3874D632"": {
""FirstName"": ""Jack"",
""LastName"": ""Andersen""
},
""34F43A33"": {
""FirstName"": ""John"",
""LastName"": ""Smith""
}
}";

var node = JSONNode.Parse(json);
if (node.Tag == JSONNodeType.Object)
{
foreach (KeyValuePair<string, JSONNode> kvp in (JSONObject)node)
{
Console.WriteLine(string.Format("{0}: {1} {2}",
kvp.Key, kvp.Value["FirstName"].Value, kvp.Value["LastName"].Value));

}
}

输出:

3874D632: Jack Andersen
34F43A33: John Smith

关于c# - 如何使用 SimpleJson c# 获取 Json 文件中的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43814309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com