澄清我的問題,我想從Namespace: Newtonsoft.Json.Linq
使用JToken.SelectTokens Method (String)
。我如何使用方法SelectTokens("")
獲得每個節點"174637"
(unit_id)和"174638"
(unit_id)?的子項數量?對於第一個節點,我應該得到1
和第二個2
。 我試過像這樣:c#json count node的孩子
foreach (var test in unit_ids) { //takes every unit_id, one by one
var children_of_unit_id = test.SelectTokens("*.[*]").count();
}
但它沒有給我什麼。
"174637": {
"1": {
"value_symbol": "3",
"exam_session_number": 1,
"exam_id": 207983,
"value_description": {
"en": "satisfactory",
}
}
}
"174638": {
"1": {
"value_symbol": "3",
"exam_session_number": 1,
"exam_id": 207984,
"value_description": {
"en": "satisfactory",
}
}
"2": {
"value_symbol": "3",
"exam_session_number": 2,
"exam_id": 207985,
"value_description": {
"en": "satisfactory",
}
}
}
EDITED
這是原Json
:
{
"grades": {
"course_units_grades": {
"173565": {
"1": {
"value_symbol": "3,5",
"exam_session_number": 1,
"exam_id": 208798,
"value_description": {
"en": "satisfactory plus",
"pl": "dst+"
}
}
},
"173566": {
"1": {
"value_symbol": "2",
"exam_session_number": 1,
"exam_id": 208797,
"value_description": {
"en": "unsatisfactory",
}
},
"2": {
"value_symbol": "3",
"exam_session_number": 2,
"exam_id": 208797,
"value_description": {
"en": "satisfactory",
}
}
}
},
"course_grades": {}
}
}
,所以它看起來是這樣的:
foreach (var t in json_grade)//take every "grades" element, one by one
{
var test = t.SelectTokens("['grades'].['course_units_grades']");
foreach (var unit_ids in test)
{
foreach (var test in unit_ids) { //takes every unit_id, one by one
var children_of_unit_id = test.SelectTokens("*.[*]").count();
}
}
}
了'json'無效。 –
我認爲你別無選擇,只能掃描json樹。我確信有很多Json.NET BFS/DFS示例。 –
我編輯了我的帖子以顯示json的原始結構以及'foreach'的外觀。 –