-3

I am consuming data from url and stored in "output" string.
How to convert that into java object and separate fields ?
Here is my code :

Client client = Client.create();
WebResource webResource = client
  .resource(url);
String name = "xxx";
String password = "xxx";
String authString = name + ":" + password;
String authStringEnc = new BASE64Encoder().encode(authString
  .getBytes());
//System.out.println("Base64 encoded auth string: " + authStringEnc);
ClientResponse response = webResource.accept("application/json")
  .header("Authorization", "Basic " + authStringEnc)
  .get(ClientResponse.class);
String output = response.getEntity(String.class);


System.out.println("Output from Server .... \n");
System.out.println(output);
Saideep
  • 1
  • 4
  • 4
    Possible duplicate of [Converting JSON to Java](https://stackoverflow.com/questions/1688099/converting-json-to-java) – U Rogel Dec 15 '17 at 10:19

1 Answers1

0

you can use gson for this. import gson to your project.

    import java.lang.reflect.Type;
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;

    //..
    //you can get the output here
    //String output = response.getEntity(String.class);

    Type listType = new TypeToken<FooObject>(){}.getType();
    FooObject fooObject =new Gson().fromJson(output, listType);
krezus
  • 1,281
  • 1
  • 13
  • 29
  • Type listType = new TypeToken(){}.getType(); Student student =new Gson().fromJson(output, listType); System.out.println(student); o/p :Student [college_name=null, college_id=0] its showing like this .i want to get particular column(college_name) values .can you please tell me the solution – Saideep Dec 15 '17 at 11:00
  • yeap, that is right. please check this [example](http://www.makeinjava.com/serialize-convert-list-objects-to-from-json-java-gson-example/) and you can modify type as object or collections. – krezus Dec 15 '17 at 11:05
  • i want that json object to store into data base .how can i send that json data to db. iam getting o/p like this:({"counts":[{"college_name":"kit", hyd","college_id":777}...............................i created table in db.i want to store these json data into that table . – Saideep Dec 15 '17 at 11:14
  • i am getting the data from client .so i want to add directly that data into db table .how cani convert thjat json data into java object and how can i save into particular columns of the table. – Saideep Dec 15 '17 at 11:16
  • ok. i know what are you trying to do it. can you post the json data that comes to **output**. You just need to modify your object to catch json data – krezus Dec 15 '17 at 11:26
  • i found the right link that you need [link](https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/) @Saideep – krezus Dec 15 '17 at 11:30
  • {"counts":[{"college_name":"Gii, Kakina","college_id":38},{"college_name":"GPT, Draksham","college_id":26}...............................i am getting the json data like this – Saideep Dec 15 '17 at 12:14
  • i want to store that json data into db table – Saideep Dec 15 '17 at 12:15