0

Issue:
I'm getting data from web-services in JSON format.I am trying to parse "quote" an "Author" but,i am unable to do it.

Here is the data:

{
"success": {
    "total": 1
},
"contents": {
    "quotes": [
        {
            "quote": "You are very powerful, provided you know how powerful you are.",

            "author": "Yogi Bhajan",

            "category": "inspire",
            "date": "2018-02-15",

            "title": "Inspiring Quote of the day"

        }
    ],
    "copyright": "2017-19 theysaidso.com"
  }
}

My Java code is :

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("quotes");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject getQuote = jsonArray.getJSONObject(i);

                            String quoteOfTheDay = getQuote.getString("quote");

                            String author = getQuote.getString("author");

                            quotesView.append(quoteOfTheDay + author + "\n\n");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

I'm certain that logic for parsing in Java class is incorrect.
Which logic should I use to correct this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

3

First, you have to get the JSONObject contents before extracting the array quotes.

try {
    JSONObject obj = response.getJSONObject("contents"); //first get the JSONObject contents
    JSONArray jsonArray = obj.getJSONArray("quotes");

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject getQuote = jsonArray.getJSONObject(i);

        String quoteOfTheDay = getQuote.getString("quote");

        String author = getQuote.getString("author");

        quotesView.append(quoteOfTheDay + author + "\n\n");
    }
} catch (JSONException e) {
    e.printStackTrace();
}
Ram Koti
  • 2,203
  • 7
  • 26
  • 36
Aman Grover
  • 1,621
  • 1
  • 21
  • 41
1

You missed contents JSONObject.

Copy past below code:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject contentsObject=response.getJSONObject("contents");
                    JSONArray jsonArray = contentsObject.getJSONArray("quotes");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject getQuote = jsonArray.getJSONObject(i);

                        String quoteOfTheDay = getQuote.getString("quote");

                        String author = getQuote.getString("author");

                        quotesView.append(quoteOfTheDay + author + "\n\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});
Vishal G. Gohel
  • 1,008
  • 1
  • 16
  • 31
1
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                      JSONObject contents = 
                     response.getJsonObject("contents");
                try {
              JSONArray quotes    = contents.getJSONArray("quotes");
           } catch (JSONException e) {
        e.printStackTrace();
       }
                    for (int i = 0; i < quotes.length(); i++) {
                        JSONObject getQuote = jsonArray.getJSONObject(i);

                        String quoteOfTheDay = getQuote.getString("quote");

                        String author = getQuote.getString("author");

                        quotesView.append(quoteOfTheDay + author + "\n\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});

Try this...

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52