-1
    Client client = Client.create();
    WebResource webResource = client
            .resource("my url");
    String name = "adn";
    String password = "34";
    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);

I am getting the o/p like this

{"counts":[{"college_name":"GPT , Karimngr","college_epass_id":18},
{"college_name":"GPT, Dra","college_epass_id":444},

I want to convert that json data type to java object can you please suggest.

vinS
  • 1,417
  • 5
  • 24
  • 37
siva
  • 9
  • 2

3 Answers3

1

Checkout this JAX-RS example.

JAX-RS is Java's inbuilt Marshalling and UnMarshalling mechanism. Where by just adding simple XML based annotations we can simply convert JSON or XML request into Java POJO models and visa -versa.

Above example will give overview of basic usage of JAX-RS REST Webservices.


Use of Jackson

ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(json, Map.class);

Your classes will be some like

public class CollegeList{
  @JsonProperty("counts")
  public List<College> counts;
}



 public class College{
     @JsonProperty("college_name") public String college_name;
     @JsonProperty("college_epass_id") public int college_epass_id;
    }

You can convert the json into respective object by

CollegeList colleges = mapper.readValue(jsonString, CollegeList .class);

Sharing Reference Link for more details.

Rohan Kadu
  • 1,311
  • 2
  • 12
  • 22
  • Hi Rohan,Thank you for giving suggestion. but i am getting the json data .i want to convert that json into java object .i created pojo class and i wrote code like this – siva Dec 18 '17 at 13:22
  • //for converting json to java object Gson gson = new GsonBuilder() .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC) .serializeNulls() .create(); Counts cou = gson.fromJson(output, Counts.class); String countArrayJson = "[{'college_name':'college__id' {'college_name':'college__id'}]"; ArrayList coun = gson.fromJson(countArrayJson, ArrayList.class); System.out.println(cou); o/p:Counts [College_name=null, College_epass_id=0] – siva Dec 18 '17 at 13:26
  • Ohhh Got it.... I had confused with your question earlier. You can also use Jack Son and I have edited Answer In order to add Code Snippets . Please have a look if it helps. – Rohan Kadu Dec 18 '17 at 14:06
  • hi rohan ,, i wrote code like this counts[] enums = gson.fromJson(output, counts[].class); System.out.println(enums); o/p:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 – siva Dec 19 '17 at 07:23
  • can you please suggest any solution for this – siva Dec 19 '17 at 07:24
  • I don't think you will be able to set array as your root. Your root will be a class and array will be member variable of that root class. So once you unmarshal your data you will get instance of class and then you can fetch arrayList using it's getter method. Hope you got my point :-) – Rohan Kadu Dec 19 '17 at 09:44
0

As you didn't specify what exactly container you use there are a lot of options, but in any case you need to create DTO (data transfer object class), get data from JSON and put in this class, then to DB.

The most straightforward is get response as string and parse with Jackson

In real java web projects modern web containers usually used, which can convert such JSON to your DTO automatically , so I propose to read documentation of your container for start

Maksym Rudenko
  • 706
  • 5
  • 16
  • Hi Maxim, i wrote code like this counts[] enums = gson.fromJson(output, counts[].class); System.out.println(enums); o/p:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 – can you please suggest any solution for this – siva Dec 19 '17 at 07:26
  • Well, this is another separate question, but anyway: you are telling gson that you expect array of counts, but an object comes in response. You are waiting for [a,b,c], but {} comes. For debugging you can parse it as string system.out and check what you receive. [related](https://stackoverflow.com/questions/20991386/expected-begin-array-but-was-begin-object-at-line-1-column-2) – Maksym Rudenko Dec 19 '17 at 09:46
0

Check out Google's Gson: http://code.google.com/p/google-gson/

From their website:

Gson gson = new Gson(); // Or use new GsonBuilder().create();
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2

You would just need to make a MyType class (renamed, of course) with all the fields in the json string. It might get a little more complicated when you're doing the arrays, if you prefer to do all of the parsing manually (also pretty easy) check out http://www.json.org/ and download the Java source for the Json parser objects.

Bennani HAMZA
  • 163
  • 2
  • 10
  • Hi Bennani, i wrote code like this counts[] enums = gson.fromJson(output, counts[].class); System.out.println(enums); o/p:Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 – can you please suggest any solution for this – – siva Dec 19 '17 at 07:26