Note that there are some explanatory texts on larger screens.

plurals
  1. POEmpty JSON response when trying to parse JSONObject with one JSONArray
    text
    copied!<p>I got code that gets <code>JSONArrays</code>, but however when I try to get <code>JSONObject</code> that contains only one <code>JSONArray</code> it gives me empty <code>JSONArray</code>.</p> <p>For example if I need to get data from this <code>JSONObject</code>:</p> <pre><code>{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""}]} </code></pre> <p>I get <code>{"events":[]}</code> as <code>JSONObject</code>, <code>[]</code> meaning that it doesn't contain any JSONArrays. Also length of <code>JSONObject</code> is in this case 0. But it doesn't throw any kind of <code>Exceptions</code>.</p> <p>but if <code>JSONObject</code> contains multiple <code>JSONArrays</code> like this:</p> <pre><code>{"events":[{"start":1357714800,"end":1357736400,"name":"Example1","description":""},{"start":1357714600,"end":1357736500,"name":"Example2","description":""},{"start":1357514800,"end":1357536400,"name":"Example3","description":""}]} </code></pre> <p>then my code works perfect.</p> <p>Here is the code I use to parse JSON:</p> <pre><code>private void getObjects(String url) throws JSONException, Exception { JSONObject jsonObject = new JSONObject(new NetTask().execute(url).get()); JSONArray job1 = jsonObject.getJSONArray("events"); System.out.println(jsonObject.toString()); System.out.println("JOB1 LENGTH: "+job1.length()); for (int i = 0; i &lt; job1.length(); i++) { JSONObject jsonEvent = job1.getJSONObject(i); int start = jsonEvent.getInt("start"); int end = jsonEvent.getInt("end"); String name = jsonEvent.getString("name"); String description = jsonEvent.getString("description"); } } public class NetTask extends AsyncTask&lt;String, Integer, String&gt; { @Override protected String doInBackground(String... params) { String jsonText = ""; BufferedReader reader = null; try { URL url = new URL(params[0]); reader = new BufferedReader(new InputStreamReader(url.openStream())); StringBuffer buffer = new StringBuffer(); int read; char[] chars = new char[1024]; while ((read = reader.read(chars)) != -1) { buffer.append(chars, 0, read); } jsonText = buffer.toString(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return jsonText; } } </code></pre> <p>is there something wrong that I am missing or is this normal behaviour?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload