0

I want to count all data using C# from this JSON

[
  {
    "p_id": "0001",
    "p_name": "Tester"
  },
  {
    "p_id": "197921",
    "p_name": "Kanza"
  }
]

How to determine that in this JSON there are 2 objects?

Emma
  • 27,428
  • 11
  • 44
  • 69

2 Answers2

0

If you use Newtonsoft.JSON you can do something like the below.

  var jsonstring = "[{'p_id': '0001','p_name': 'Tester'},{'p_id': '197921','p_name': 'Kanza'}]";
  var json = JsonConvert.DeserializeObject<JArray>(jsonstring);
  int length = json.Count;

length will give you the number of items in your JSON array. Proof is below Proof of length of JSON object

K Emery
  • 91
  • 2
  • 12
0

You can get the length like this:

var jsonstring = "[{'p_id': '0001','p_name': 'Tester'},{'p_id': '197921','p_name': 'Kanza'}]"; int length = ((JArray)jsonstring).Count;