【问题标题】:c# json count node's childrenc# json 计算节点的子节点
【发布时间】:2015-07-19 09:49:39
【问题描述】:

澄清我的问题,我想使用来自Namespace: Newtonsoft.Json.LinqJToken.SelectTokens Method (String)。如何使用方法 SelectTokens("") 获取每个节点 "174637"(unit_id) 和 "174638"(unit_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();
        }
    }
}

【问题讨论】:

  • json 无效。
  • 我认为你别无选择,只能扫描 json 树。我敢肯定那里有很多 Json.NET BFS/DFS 示例。
  • 我已经编辑了我的帖子以显示 json 的原始结构以及foreach 的样子。
  • 你不能deserializejson吗?

标签: c# json linq json.net


【解决方案1】:

您可以尝试以下两种方法之一:

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

Dotnetfiddle Demo

【讨论】:

    【解决方案2】:

    试试这个:

     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"
            }
          }
        }
      ]
    }
    

    【讨论】:

    • j 是您正在解析的json
    • 谢谢!我需要var token = JToken.Parse(j)["unit_id"].ToList().Count; 来计算 json 条目。
    猜你喜欢
    • 1970-01-01
    • 2020-05-26
    • 2016-10-08
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多