-1
{ 
   "quote": "To understand the heart and mind of a person, look not at 
   what he has already achieved, but at what he aspires to.",
   "author": "Kahlil Gibran (1883-1931)"
 }

How to parse this with no any object name

3 Answers3

1

There are different methods to parse json object

Simple way

JSONObject myJson = new JSONObject(yourJsonString);
String quote = myJson. getString("quote");
String author = myJson. getString("author");

Another and recomented way is parse json using Gson For this you can refer below links

How to parse json parsing Using GSON in android

https://www.journaldev.com/2321/gson-example-tutorial-parse-json

EKN
  • 1,886
  • 1
  • 16
  • 29
0

How to do it ?

  • If you meet {} in your code , you can use JSONObject to parse it .

  • If you meet [] in your code , you can use JSONArray to parse it .

  • And if you meet [] in your code , you can use for loop to get value in it .

  • And you should use try catch in your code .

Try this .

try {
        JSONObject jsonObject = new JSONObject(response);
        String author = jsonObject.getString("author");
        String quote = jsonObject.getString("quote");
} catch (JSONException e) {
        e.printStackTrace();
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

This is absolutely normal json.

JSONObject json = new JSONObject(your_json);
String quote = json.getString("quote");
String author = json.getString("author");
Northern Poet
  • 1,955
  • 1
  • 12
  • 17