澄清我的问题,我想使用命名空间:Newtonsoft.Json.Linq
中的JToken.SelectTokens方法(字符串)
。如何使用方法 SelectTokens("")
获取每个节点 "174637"
(unit_id) 和 "174638"
(单位 ID) ?.对于第一个节点,我应该得到 1
,对于第二个节点,我应该得到 2
。
我试过这样:
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",
}
}
}
已编辑
这是原始的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();
}
}
}
最佳答案
您可以尝试以下两种方法之一:
foreach (var test in unit_ids)
{
var approach1 = test.Children().Children().Count();
var approach2 = test.First.SelectTokens("*").Count();
}
关于时间:2019-03-17 标签:c#jsoncountnodeschildren,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31499933/