Asad Asad - 6 months ago 66
JSON Question

return null while parsing json in loop

When I'm trying to parse "publisher" from json object it is returning error but if I parse only "title" it works fine even "title" and "publisher" are siblings.

public ArrayList<BookData> parseJSON(String jsonString){
JSONObject obj = new JSONObject(jsonString);
JSONArray jsonArray = obj.getJSONArray("items");
for(int i=0;i<10;i++){
JSONObject object = jsonArray.getJSONObject(i);
JSONObject volumeInfo = object.getJSONObject("volumeInfo");
//this title works fine
String title = volumeInfo.getString("title");
//this publisher is throwing null pointer exception
String publisher = volumeInfo.getString("publisher");
//Log.v("testing title", publisher);
list.add(new BookData(title, "publisher"));

return list;

}catch (Exception e){e.printStackTrace();}
return null;

JSON is here:

"title":"Organic Chemistry",
"Jonathan Clayden",
"Nick Greeves",
"Stuart Warren"
"publisher":"Oxford University Press"


list.add(new BookData(title, "publisher")); replace this with below given code.

list.add(new BookData(title, publisher));