Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the response String I get from a server:

{

  "name": "Json",

  "detail": {

    "first_name": "Json",

    "last_name": "Scott",

    "age": "23"

  },

  "status": "success"

}

Can anyone tell me how to get the value of the first name?

1 Answer

0 votes
by (19.7k points)

Check out the code I used for my application:

String data="{'foo':'bar','coolness':2.0, 'altitude':39000, 'pilot':{'firstName':'Buzz','lastName':'Aldrin'}, 'mission':'apollo 11'}";

To retrieve the first name, I do something like the below: 

JSONObject json = (JSONObject) JSONSerializer.toJSON(data);        

    double coolness = json.getDouble( "coolness" );

    int altitude = json.getInt( "altitude" );

    JSONObject pilot = json.getJSONObject("pilot");

    String firstName = pilot.getString("firstName");

    String lastName = pilot.getString("lastName");

    System.out.println( "Coolness: " + coolness );

    System.out.println( "Altitude: " + altitude );

    System.out.println( "Pilot: " + lastName );

Interested in Java? Check out this Java tutorial by Intellipaat.  

Related questions

Browse Categories

...