Accessing all items in the JToken(访问 JToken 中的所有项目)
问题描述
我有一个这样的 json 块:
I have a json block like this:
{
"ADDRESS_MAP":{
"ADDRESS_LOCATION":{
"type":"separator",
"name":"Address",
"value":"",
"FieldID":40
},
"LOCATION":{
"type":"locations",
"name":"Location",
"keyword":{
"1":"LOCATION1"
},
"value":{
"1":"United States"
},
"FieldID":41
},
"FLOOR_NUMBER":{
"type":"number",
"name":"Floor Number",
"value":"0",
"FieldID":55
},
"self":{
"id":"2",
"name":"Address Map"
}
}
}
如何获取此令牌包含的所有关键项目.例如,从上面的代码中,我想要 "ADRESS_LOCATION" 、 "LOCATION"、"FLOOR_NUMBER" 和 "self".
How can I get all the key items that this token includes. For example from the above code I want to have "ADRESS_LOCATION" , "LOCATION", "FLOOR_NUMBER" and "self".
推荐答案
您可以将 JToken
转换为 JObject
,然后使用 Properties()
方法来获取对象属性的列表.从那里,您可以很容易地获得名称.
You can cast your JToken
to a JObject
and then use the Properties()
method to get a list of the object properties. From there, you can get the names rather easily.
类似这样的:
string json =
@"{
""ADDRESS_MAP"":{
""ADDRESS_LOCATION"":{
""type"":""separator"",
""name"":""Address"",
""value"":"""",
""FieldID"":40
},
""LOCATION"":{
""type"":""locations"",
""name"":""Location"",
""keyword"":{
""1"":""LOCATION1""
},
""value"":{
""1"":""United States""
},
""FieldID"":41
},
""FLOOR_NUMBER"":{
""type"":""number"",
""name"":""Floor Number"",
""value"":""0"",
""FieldID"":55
},
""self"":{
""id"":""2"",
""name"":""Address Map""
}
}
}";
JToken outer = JToken.Parse(json);
JObject inner = outer["ADDRESS_MAP"].Value<JObject>();
List<string> keys = inner.Properties().Select(p => p.Name).ToList();
foreach (string k in keys)
{
Console.WriteLine(k);
}
输出:
ADDRESS_LOCATION
LOCATION
FLOOR_NUMBER
self
这篇关于访问 JToken 中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:访问 JToken 中的所有项目
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01