title: JSONObject JSON array length
body: <p>I am reading the below XML in java, and converting to <code>JSONObject</code> [<code>org.json.JSONObject</code>].
  and appending the metadata and returning as JSON.</p>
  
  <p><strong>Problem</strong>: 
  <code>JSONObject jsonObj = service.getMyData();</code> // custom methods and invocation</p>
  
  <p><code>int count = jsonObj.length();</code> // length is 1 </p>
  
  <p>How do I take the correct count from this jsonObj? . The correct count I am referring to is 2 in this case. [2 books]</p>
  
  <p>What I tried?</p>
  
  <p>(1) <code>System.out.println(jsonObj.length());</code> // prints 1</p>
  
  <p>API says: length() 
            Get the number of keys stored in the JSONObject.</p>
  
  <p>(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 <code>org.json.JSONException: JSONObject["User"] not found.</code></p>
  
  <pre><code> System.out.println(jsonObj.getJSONArray("User").length());
  </code></pre>
  
  <p>(3) IF I try 'Users' ie: <code>jsonObj.getJSONArray("Users").length()</code> , it says <code>JSONObject["Users"] is not a JSONArray</code>.</p>
  
  <p>(4) </p>
  
  <pre><code>JSONObject jsonObj = service.getMyData();
   JSONObject jUsers =  jsonObj.getJSONObject("Users");
   JSONObject jUser =  jUsers.getJSONObject("User");
   System.out.println(jUser.length());
  </code></pre>
  
  <p>Error: <code>org.json.JSONException: JSONObject["User"] is not a JSONObject.</code></p>
  
  <hr>
  
  <p><strong>XML:</strong></p>
  
  <pre><code>&lt;Users&gt;
      &lt;User&gt;
          &lt;Name&gt;Unni&lt;/Name&gt;
          &lt;Books&gt;
              &lt;Book&gt;book1&lt;/Book&gt;
              &lt;Book&gt;book2&lt;/Book&gt;
              &lt;Book&gt;book3&lt;/Book&gt;
          &lt;/Books&gt;
      &lt;/User&gt;
      &lt;User&gt;
          &lt;Name&gt;Ammu&lt;/Name&gt;
          &lt;Books&gt;
              &lt;Book&gt;book1&lt;/Book&gt;
              &lt;Book&gt;book2&lt;/Book&gt;
              &lt;Book&gt;book4&lt;/Book&gt;
          &lt;/Books&gt;
      &lt;/User&gt;
  &lt;/Users&gt;
  </code></pre>
  
  <p><strong>JSON:</strong></p>
  
  <pre><code>{
      "Users": {
          "User": [
              {
                  "Name": "Unni",
                  "Books": {
                      "Book": [
                          "book1",
                          "book2",
                          "book3"
                      ]
                  }
              },
              {
                  "Name": "Ammu",
                  "Books": {
                      "Book": [
                          "book1",
                          "book2",
                          "book4"
                      ]
                  }
              }
          ]
      }
  }
  </code></pre>