JSONObject JSON array length

19,704

Solution 1

In JSON format,

[] is JSONArray

{} is JSONObject

In your post,

  • Users - JSONObject
  • User - JSONArray
  • Books - JSONObject
  • Book - JSONArray

Error: org.json.JSONException: JSONObject["User"] is not a JSONObject.

As mentioned above, try with getJSONArray()

 JSONArray userArray = jUsers.getJSONArray("User");

Solution 2

I had to call the getJSONArray method when retrieving the array.

JSONObject jsonObj = service.getMyData();
JSONObject jUsers =  jsonObj.getJSONObject("Users");
System.out.println(jUsers.getJSONArray("User").length());
Share:
19,704
spiderman
Author by

spiderman

#SOreadytohelp

Updated on July 25, 2022

Comments

  • spiderman
    spiderman almost 2 years

    I am reading the below XML in java, and converting to JSONObject [org.json.JSONObject]. and appending the metadata and returning as JSON.

    Problem: JSONObject jsonObj = service.getMyData(); // custom methods and invocation

    int count = jsonObj.length(); // length is 1

    How do I take the correct count from this jsonObj? . The correct count I am referring to is 2 in this case. [2 books]

    What I tried?

    (1) System.out.println(jsonObj.length()); // prints 1

    API says: length() Get the number of keys stored in the JSONObject.

    (2) Since number of keys is returned as 1, there is no point on trying the below one, still I tried and got the error of org.json.JSONException: JSONObject["User"] not found.

     System.out.println(jsonObj.getJSONArray("User").length());
    

    (3) IF I try 'Users' ie: jsonObj.getJSONArray("Users").length() , it says JSONObject["Users"] is not a JSONArray.

    (4)

    JSONObject jsonObj = service.getMyData();
     JSONObject jUsers =  jsonObj.getJSONObject("Users");
     JSONObject jUser =  jUsers.getJSONObject("User");
     System.out.println(jUser.length());
    

    Error: org.json.JSONException: JSONObject["User"] is not a JSONObject.


    XML:

    <Users>
        <User>
            <Name>Unni</Name>
            <Books>
                <Book>book1</Book>
                <Book>book2</Book>
                <Book>book3</Book>
            </Books>
        </User>
        <User>
            <Name>Ammu</Name>
            <Books>
                <Book>book1</Book>
                <Book>book2</Book>
                <Book>book4</Book>
            </Books>
        </User>
    </Users>
    

    JSON:

    {
        "Users": {
            "User": [
                {
                    "Name": "Unni",
                    "Books": {
                        "Book": [
                            "book1",
                            "book2",
                            "book3"
                        ]
                    }
                },
                {
                    "Name": "Ammu",
                    "Books": {
                        "Book": [
                            "book1",
                            "book2",
                            "book4"
                        ]
                    }
                }
            ]
        }
    }