JSON array get length

后端 未结 7 951
长情又很酷
长情又很酷 2021-01-17 08:25

I can\'t get the array length of the JSON array. when I using length(); method it is not working, it show as red underline in Netbeans

this is the source code

相关标签:
7条回答
  • 2021-01-17 08:31

    The below snippet works fine for me(I used the size())

    String itemId;
                for (int i = 0; i < itemList.size(); i++) {
                JSONObject itemObj = (JSONObject)itemList.get(i);
                itemId=(String) itemObj.get("ItemId");
                System.out.println(itemId);
                }
    

    If it is wrong to use use size() kindly advise

    0 讨论(0)
  • 2021-01-17 08:32

    The JSONArray.length() returns the number of elements in the JSONObject contained in the Array. Not the size of the array itself.

    0 讨论(0)
  • 2021-01-17 08:35

    I came here and looking how to get the number of elements inside a JSONArray. From your question i used length() like that:

    JSONArray jar = myjson.getJSONArray("_types");
    System.out.println(jar.length());
    

    and it worked as expected. On the other hand jar.size();(as proposed in the other answer) is not working for me.

    So for future users searching (like me) how to get the size of a JSONArray, length() works just fine.

    0 讨论(0)
  • 2021-01-17 08:47

    have you tried size() ? Something like :

    jar.size();
    

    Hope it helps.

    0 讨论(0)
  • 2021-01-17 08:49

    Note: if you're using(importing) org.json.simple.JSONArray, you have to use JSONArray.size() to get the data you want. But use JSONArray.length() if you're using org.json.JSONArray.

    0 讨论(0)
  • 2021-01-17 08:52

    At my Version the function to get the lenght or size was Count()

    You're Welcome, hope it help someone.

    0 讨论(0)
提交回复
热议问题