Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Length of JSON Array

I am using UnityWebRequest to get a query and parse it in JSON. Everything works as expected but here the node users is an array that is defined as below:

{
  "data": {
    "users": [
      {
        "id": "981d8432-c423-46e1-9124-5b5f111bd749",
      },
      {
        "id": "11cd2db5-3e6e-4363-b8e5-afd2a67a5333",
      }
   ]
}

Now I know that the array is 2 values but how do I make the for loop detect it by the length? Such that

using SimpleJSON;
...
...
void Start()
{

  JSONNode itemsData = JSON.Parse (request.downloadHandler.text);
  for(int i = 0;i<(LengthOfUsers); i++)
    {
      Debug.Log("\nIDs: "+ itemsData["data"]["users"][i]["id"]);
    }
}
like image 365
Amar.linsila Avatar asked Sep 02 '25 04:09

Amar.linsila


1 Answers

Seems like JSonNode implements IEnumerator: https://wiki.unity3d.com/index.php/SimpleJSON

That means that you could in theory use the Linq extentions like this:

itemsData["data"]["users"].Count
like image 115
Athanasios Kataras Avatar answered Sep 04 '25 18:09

Athanasios Kataras