user1050619
user1050619

Reputation: 20916

Jersey client to process JSON data

I'm trying to create a simple Jersey client to process data from a public API.

Using the below program, Im able to read and process the data but I want to convert the JSON response to Java Object so that I have a structure.

How do I build the Java object structure based on the response.

public class RestServiceClient {

    public static void main(String[] args) {
        Client client = Client.create();
        WebResource webResource2 = client.resource("https://data.montgomerycountymd.gov/api/views/54rh-89p8/rows.json?accessType=DOWNLOAD");
        ClientResponse response2 = webResource2.accept("application/json").get(ClientResponse.class);
        if (response2.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response2.getStatus());
        }

        String output2 = response2.getEntity(String.class);
        System.out.println(output2);

    }


}

Upvotes: 0

Views: 4217

Answers (2)

Arundev
Arundev

Reputation: 1944

Please use the below code for send data to a rest URL and get the desired object back. (Expect JSON FORMAT if not change the mediatype proeprty to XML )

   //packages for your reference
   import com.sun.jersey.api.client.Client;
   import com.sun.jersey.api.client.WebResource;
   import com.sun.jersey.api.client.config.ClientConfig;
   import com.sun.jersey.api.client.config.DefaultClientConfig;
   import com.sun.jersey.multipart.impl.MultiPartWriter;

   //Java Code
    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(MultiPartWriter.class);
    final Client client = Client.create(cc);

    WebResource resource = client.resource("https://your_url");

    //class that we the rest end point expecting
    CustomInputClass input = new CustomInputClass();
    // set data to input

    CustomOutputClass output = resource
                .accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
                .post(CustomOutputClass.class,input);

Upvotes: 1

tom
tom

Reputation: 1493

Try something like this (using Google Gson parsing). It doesn't quite parse it 100%, but it's a start.

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

    Client client = Client.create();
    WebResource webResource2 = client.resource("https://data.montgomerycountymd.gov/api/views/54rh-89p8/rows.json?accessType=DOWNLOAD");
    ClientResponse response2 = webResource2.accept("application/json").get(ClientResponse.class);
    if (response2.getStatus() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response2.getStatus());
    }

    String output2 = response2.getEntity(String.class);
    JsonParser parser = new JsonParser();
    JsonElement parsed = parser.parse(output2);
    JsonObject asJsonObject = parsed.getAsJsonObject();
    JsonArray data = asJsonObject.getAsJsonArray("data");
    for(int i=0; i<data.size(); i++) {
        JsonElement jsonElement = data.get(i);
        JsonArray subdata = jsonElement.getAsJsonArray();
        for(int j=0; j<subdata.size(); j++) {
            JsonElement subelement = subdata.get(j);
            if(subelement.isJsonNull()) {
                break;
            }
            System.out.print(subelement.getAsString() + ",");
        }
        System.out.println();
    }

Upvotes: 4

Related Questions