Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your issue is with your JSONParser..that and the JSON returned from the site isnt well formed. You have a bunch of JSONObjects with JSONObjects inside of them that have similar names. I made a quick and easy solution to deal with it but if you control the code that generates the JSON then I would suggest grouping the categories of items (books, music, camera) into their own JSONArrays</p> <p>The JSON you're getting back from the website is a JSONArray with JSONObjects.. you're doing the following</p> <p>jObj = new JSONObject(json);</p> <p>You should be doing </p> <p>jObj = new JSONArray(json);</p> <p>Then you need to change your return to be a JSONArray instead of JSONObject</p> <p>Then in the main activity change "json" to JSONArray and get rid of "book"..just loop through the JSONArray</p> <p>Also move your try to be inside of the loop. </p> <pre><code>JSONParser jParser = new JSONParser(); // getting Json String from url JSONArray json = jParser.getJSONFromUrl(url); // getting array of books //Get rid of this = book = json.getJSONArray(TAG_BOOK); // looping through all books for(int i=0;i&lt;json.length();i++){ try{ JSONObject parent = json.getJSONObject(i); JSONObject c = parent.optJSONObject("camera"); if(c == null) c = parent.optJSONObject("book"); if(c == null) c = parent.optJSONObject("music"); // storing each json item in variable String title = c.getString(TAG_TITLE); String price = c.getString(TAG_PRICE); String authors = c.getString(TAG_AUTHORS); String id = c.getString(TAG_ID); String description = c.getString(TAG_DESCRIPTION); // creating new HashMap HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put(TAG_ID, id); map.put(TAG_TITLE, title); map.put(TAG_PRICE, price); map.put(TAG_AUTHORS, authors); map.put(TAG_DESCRIPTION, description); bookList.add(map); } catch (JSONException e) { e.printStackTrace(); } } </code></pre> <p>-----EDIT</p> <p>As suggested in my comment, you should redo the code that creates the JSON output</p> <p>In simple terms, you need a master array that contains JSONObjects for each "category", and then each of those categories will be a JSONObject in a JSONArray</p> <p>The JSON should look like this</p> <pre><code>[ { "camera" : [ { "picture":"http://qqq.com", "price":9.99 }, { "picture":"http://eee.com", "price":9.99 } ] }, { "music" : [ { "picture":"http://aaa.com", "price":9.99 }, { "picture":"http://sss.com", "price":9.99 } ] }, { "book" : [ { "picture":"http://fff.com", "price":9.99 }, { "picture":"http://ggg.com", "price":9.99 } ] } ] </code></pre> <p>This way you can do different actions (if needed) for the different categories and have any similar JSON Objects in the category all grouped together.</p> <p>Or if the action will be the same regardless if it's a book, music or camera then you can just add another field to your JSON Objects for "type" and then create the JSONObject in the array (JSON_array.put()) without a name so it would appear as</p> <pre><code>[ { "type":"camera", "picture":"http://img5.flixcart.com/image/camera/5/p/a/nikon-d90-slr-40x40-imacxmh3y6wmqh8b.jpeg", "model":"D90 SLR with AF-S 18-105mm VR Kit Lens", "make":"Nikon", "price":57182.0 }, { "type":"book", "description":"This book is one of a series of texts written by faculty of the Electrical Engineering and Computer Science Department at the Massachusetts Institute of Technology. It was edited and produced by The MIT Press under a joint production-distribution arrangement with the McGraw-Hill Book Company.", "authors":"Harold Abelson and Gerald Jay Sussman with Julie Sussman", "price":469.0, "id":51087, "title":"Structure and Interpretation of Computer Programs" } ] </code></pre> <p>Again, the route you should take depends on what you'll be doing with the data afterwards </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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