如何使用简单 Json c# 获取 Json 文件中的密钥
How To Get Key in a Json File with SimpleJson c#
我有一个 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
我有一个 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