我无法确切地知道如何通过此JObject来在运行时检索id属性。
我有以下代码,可以成功为我提供条目下的id属性,但是如何再次嵌套它以进入运行部分并获取这些ID?
JSON:
{
"id": 168,
"name": "section 1",
"entries": [
{
"id": "908-9876-908",
"suite_id": 15,
"name": "List 1",
"runs": [
{
"id": 169,
"suite_id": 15
}
]
},
{
"id": "998-4344-439",
"suite_id": 16,
"name": "List 2",
"runs": [
{
"id": 170,
"suite_id": 16
}
]
}
]
}
C#代码:
JObject obj = JsonConvert.DeserializeObject<JObject>(response);
foreach (JObject id in obj["entries"])
{
string returnable = (string)id["id"];
Console.WriteLine(returnable);
}
我尝试查看[“ entries”] [“ runs”],但那也没有用。
打印出来的是:
908-9876-908
998-4344-439
我想要的是
169
170
您可以使用以下代码来实现
var jsonObject = JObject.Parse(json);
foreach (var entry in jsonObject["entries"])
{
foreach (var run in entry["runs"])
{
string returnable = (string)run["id"];
Console.WriteLine(returnable);
}
}
你想看
169
170
它们是数组中的id
值runs
,因此应在内部循环中枚举它们。您还错过了逗号"name": "section 1"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句