简体   繁体   中英

c# json count node's children

clarifying my question, I would like to use JToken.SelectTokens Method (String) from Namespace: Newtonsoft.Json.Linq . How can I use method SelectTokens("") to get number of children for every node "174637" (unit_id) and "174638" (unit_id) ?. For first node I'm supposed to get 1 and for second 2 . I've tried like this:

foreach (var test in unit_ids) { //takes every unit_id, one by one
     var children_of_unit_id = test.SelectTokens("*.[*]").count();
}

But It gives me nothing.

"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

This is original 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": {}
  }
}

So It looks like this:

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();
        }
    }
}

Try this:

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

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

You can try one of these 2 ways :

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

Dotnetfiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM