2

I'm trying to extract data from Github Sample Collection of Books but i am getting a blank screen. here is my JSON parsing code.

try {

        JSONObject bookObject = new JSONObject(SAMPLE);
        JSONArray booksArray = bookObject.getJSONArray("books");

        for (int i = 0; i < booksArray.length(); i++){

           JSONObject currentBook = booksArray.getJSONObject(i);

           String title =  currentBook.getString("title");
           String author = currentBook.getString("author");
           String isbn = currentBook.getString("isbn");

           Book book = new Book(title,author,isbn);
           books.add(book);
        }

}catch (JSONException e){
        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
2
  • 1
    SAMPLE contains a json string similar to the link? What exactly you want to achieve and what you mean by "all I get is a blank screen"? Commented Jul 25, 2019 at 10:21
  • Consider using Gson, create a Java object that corresponds to the json and then simply convert the json string to the object. Commented Jul 25, 2019 at 10:34

4 Answers 4

4

I recommend to you use GSON, is very easy library

To Json

Gson gson = new Gson();

Staff obj = new Staff();

// 1. Java object to JSON file
gson.toJson(obj, new FileWriter("C:\\projects\\staff.json"));

// 2. Java object to JSON string
String jsonInString = gson.toJson(obj);

From Json

Gson gson = new Gson();

// 1. JSON file to Java object
Staff staff = gson.fromJson(new FileReader("C:\\projects\\staff.json"), Staff.class);

// 2. JSON string to Java object
String json = "{'name' : 'mkyong'}";
Staff staff = gson.fromJson(json, Staff.class);

// 3. JSON file to JsonElement, later String
JsonElement json = gson.fromJson(new FileReader("C:\\projects\\staff.json"), JsonElement.class);
String result = gson.toJson(json);

If you want to see more information about this you can check this link: https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Sign up to request clarification or add additional context in comments.

Comments

2

Try this :

    Gson gson = new Gson();

    JSONObject o = new JSONObject(jsonData.toString()); // pass your data here
    JSONArray arr = new JSONArray(o.get("books").toString());

    List<Book> books = new ArrayList<Book>();

    for (int i = 0; i < arr.length(); i++) {
        Book book = gson.fromJson(arr.get(i).toString(), Book.class);
        books.add(book);
    }

I have used Gson library here. There are other libraries as well. Refer this link for more details: http://tutorials.jenkov.com/java-json/gson-jsonparser.html

Comments

1

you need to convert the response from the network service to string and then get the jsonArray it will work

Like this ::

@Override
public void onResponse(Call call, final okhttp3.Response response) throws IOException {
                    final String stringResponse = response.body().string();
                    //insted of sample pass the stringresponse it will work
                    JSONObject bookObject = new JSONObject(stringResponse);
                    JSONArray booksArray = bookObject.getJSONArray("books");

            for (int i = 0; i < booksArray.length(); i++){

                JSONObject currentBook = booksArray.getJSONObject(i);

               String title =  currentBook.getString("title");
               String author = currentBook.getString("author");
               String isbn = currentBook.getString("isbn");

                Book book = new Book(title,author,isbn);
                books.add(book);
            }
            });

Comments

0

Check your model class , in that you set the parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.