2015-07-19 40 views
1

澄清我的问题,我想从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(); 
     } 
    } 
} 
+0

了'json'无效。 –

+0

我认为你别无选择,只能扫描json树。我确信有很多Json.NET BFS/DFS示例。 –

+0

我编辑了我的帖子以显示json的原始结构以及'foreach'的外观。 –

回答

1

您可以尝试这些2种方式之一:

foreach (var test in unit_ids) 
{ 
    var approach1 = test.Children().Children().Count(); 
    var approach2 = test.First.SelectTokens("*").Count(); 
} 

Dotnetfiddle Demo

0

试试这个:

var token = JToken.Parse(j)["unit_id"][0].ToList().Count; 

样品JSON:

{ 
    "174637": [ 
    { 
     "1": { 
     "value_symbol": "3", 
     "exam_session_number": "1", 
     "exam_id": "207983", 
     "value_description": { 
      "en": "value_description" 
     } 
     } 
    } 
    ], 
    "174638": [ 
    { 
     "1": { 
     "value_symbol": "3", 
     "exam_session_number": "1", 
     "exam_id": "207983", 
     "value_description": { 
      "en": "value_description" 
     } 
     }, 
     "2": { 
     "value_symbol": "3", 
     "exam_session_number": "1", 
     "exam_id": "207983", 
     "value_description": { 
      "en": "value_description" 
     } 
     } 
    } 
    ] 
} 
+0

'j'是你正在解析的'json'。 –