Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: JSONObject only parses the final object in file
    text
    copied!<p>I have a json file formatted like this (only the actual file has no whitespace):</p> <pre><code>{ "main": [ { "sections": [ "sec1", "sec2" ], "title": "Section List 1" }, { "sections": [ "sec3", "sec4", "sec5" ], "title": "Section List 2" } ], "sections": { "sec1": { "products": [ "prod1", "prod2" ] }, "sec2": { "products": [ "prod3" ] } }, "products": { "prod1": { "url": "url1.gif", "title": "Product 1" }, "prod2": { "url": "url2.gif", "title": "Product 2" }, "prod3": { "url": "url3.gif", "title": "Product 3" } } } </code></pre> <p>When I attempt to send the data for that file into JSONObject, the JSONObject being loaded only contains the final object in the top list, in this case "products". Right now, my code to load the JSONObject is this:</p> <pre><code>JSONObject dlResult = new JSONObject(new Scanner(cnxn.getInputStream()).nextLine()); </code></pre> <p>I've also tried storing the data in a String first and giving that to the JSONObject constructor, I've tried giving that String to a JSONTokener first, and giving that tokener to the JSONObject constructor. Both the String and the JSONTokener contain the entire file, but once it gets put into the JSONObject, it's always the same thing - "main" and "sections" get cut out, and only "products" remains.</p> <p>Here's the rest of my relevant code:</p> <pre><code>public class MapsListFragment extends ListFragment { private static JSONObject mDlResult; private ArrayAdapter&lt;MapInfo&gt; mMapListAdapter = null; private class MapInfo { private String mText; private String mURL; MapInfo(String inText, String inURL) { mText = inText; mURL = inURL; } public String URL() { return mURL; } @Override public String toString() { return mText; } } public class MapsListUpdater extends AsyncTask&lt;String, String, JSONObject&gt; { private static final String URL = "http://jsonURL/products.json"; private Date lastUpdate; @Override protected JSONObject doInBackground(String... inObjects) { Date ifModifiedSince = lastUpdate; try { HttpURLConnection cnxn = (HttpURLConnection) new URL(URL).openConnection(); if (ifModifiedSince != null) cnxn.setIfModifiedSince(ifModifiedSince.getTime()); cnxn.connect(); if (cnxn.getResponseCode() != HttpURLConnection.HTTP_NOT_MODIFIED) { mDlResult = new JSONObject(new Scanner(cnxn.getInputStream()).nextLine()); } cnxn.disconnect(); } catch (Exception e) { boolean check = true; } return mDlResult; } @Override protected void onPostExecute(JSONObject result) { super.onPostExecute(result); try { // This is really ugly and brute-forcey, but it's the only way I could get JSONObjects populated with ALL the data! /*String[] tryThis = mDlResult.split(",\"sections\":"); tryThis[0] += '}'; tryThis[1] = tryThis[1].substring(0, tryThis[1].indexOf("]}},\"products\":")); tryThis[1] = "{\"sections\":" + tryThis[1] + "]}}}";*/ JSONObject mainObj = mDlResult.getJSONObject("main");//new JSONObject(mDlResult); JSONArray mainArray = mainObj.getJSONArray("main"); Vector&lt;String&gt; titles = new Vector&lt;String&gt;(); mMapListAdapter.clear(); for (int i = 0; i &lt; mainArray.length(); i++) { JSONObject object = mainArray.getJSONObject(i); String title = object.getString("title"); if(!titles.contains(title)) { titles.add(title); mMapListAdapter.add(new MapInfo(object.getString("title"), null)); } } } catch (Exception e) { boolean check = true; } } } } </code></pre>
 

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