0
{  
   status:"success",
   user:{  
      id:"86",
      user_id:"83",
      user_profile_id:"777",
      first_name:"xxxx",
      last_name:"x1",
      full_name:"cccc2",
      country:"IN",
      state:"Karnataka",
      city:"Bangalore Urban",
      zip:"560052",
      timezone:null,
      timezone_id:null,
      photo:null,
      import_key:null,
      date_update:null,
      role:"22",
      status:"10",
      email:"mail@gmail.com",
      email_is_confirmed:"1",
      reg_date:"2016-03-18 11:08:32",
      last_login_date:"2016-03-18 11:10:40"
   },
   token:"iuwehry723ibf8f289ryfhu",
   saved:"0"
}

My API returns the above output. How can I print each value like so:

id = 86
user_id = 83
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
BugBaster
  • 71
  • 1
  • 3
  • 11

2 Answers2

0

First of all you need to have the library... you can find it here And This is what you have in this library

You download it, extract it and copy the jar file to your project, then right click on it and add it to your build path

If you have the json in a String, you can do like this

// Load the json string as an Json object
JSONObject obj = new JSONObject(yourStringJson);


// Extract from this object whatever is in

// if is a String
String status = obj.getString("status");

// if is another object, you get the object first and the whatever is inside
String id = obj.getJSONObject("user").getString("id");
String userId = obj.getJSONObject("user").getString("user_id");
.....

// and so on
String token = obj.getString("token");
jonhid
  • 2,075
  • 1
  • 11
  • 18
  • JSONParser parser = new JSONParser(); JSONObject obj = new JSONObject(jsonString); (Eclipse shows The constructor JSONObject(String) is undefined) – BugBaster Mar 20 '16 at 16:53
0

You can use Goolge Gson api to print your Json object

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>1.7.1</version>
</dependency>

Gson gson = new Gson();
String json = gson.toJson(obj);
System.out.println(json);
Linh Nguyen
  • 221
  • 2
  • 8