Take the 2-minute tour ×

Typically I have been using xml web services and have used JAXB for marshalling/unmarshalling the xml. In this case, I have an xml schema for generating the classes and then at runtime I simply deal with java objects and not with xml since that unmarshalling is done under the covers for me.

What sort of libraries exist for doing something similar in json? I have to make an http Get call that returns a list of json objects and I am not sure what my java client should look like. What are the best practices here?


share|improve this question
add comment

3 Answers

String url = "www.abc.com/products/1"; // 1 = product number
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
request.addHeader("User-Agent", USER_AGENT);
new UsernamePasswordCredentials("username", "password"), "UTF-8", false));
HttpResponse response = client.execute(request);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
data = new String(rd.readLine());

Now you have to creae object of JSon library to parse the data or fetch particular data.

try {
        obj = new JSONObject(data);
        System.out.println("Final Data" + data);
        String category = obj.getString("product");
        System.out.println("category " + category);
} catch (JSONException ex) {

Use org.codehaus.jettison.json library to deal with json data.

share|improve this answer
add comment

There are a lot of Java libraries out there that parse JSON. The simplest one to use is the one by org.json. For a list of various JSON parsing libraries take a look at (http://json.org)

If you have used JAXB to marshall / unmarshall value objects (POJO's) then you can also take a look at Jackson which uses JAXB annotations to determine what your POJO's representation in JSON should look like. (http://jackson.codehaus.org/)

Essentially you would need to make a HTTP GET request for your JSON, parse the request using a JSON parser and convert them into a convenient representation in Java. You can use the Apache Commons HttpClient libraries and JSON parsers in conjunction.

share|improve this answer
add comment

If you need to create both RESTful service and its client, then I would prefer rest4j. You can describe your RESTful service using XML and implement your service as it was a plain Spring bean. You can then also generate documentation and java/python/php/c# client libraries. Marshaling and unmarshalling is done under the hood.

And there is a way to flexibly map your internal Java objects into external JSON representation, like renaming a property, exporting only a subset of properties etc.

share|improve this answer
add comment

Your Answer


