Using JSON Web Service in Java Program

5/24/2012 code-examplejava

# Summary

Before knowing on how to use the JSON Web Service, you can read this article on how to create JSON Web Service.

So for using JSON Web Service, you need to have an external library imported into your project which can be used easily from JSON Documentation (opens new window).

Also to make HTTP request easily, we can make use Apache Java library for the same which can be downloaded from Apache Downloads (opens new window)

Once your import's are done, you can use the following code to use the json data taken from a webservice.

# Screenshot

Using JSON Web Service in Java

# HTTPJsonTest.java

package httpjsontest;

import org.json.JSONArray;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

/**
 *
 * @author RohanSakhale
 */
public class HTTPJsonTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // Create HttpClient imported from apache library
        HttpClient client = new DefaultHttpClient();
        // Create get object for that by giving your web service URL
        HttpGet get = new HttpGet("http://demo.rohansakhale.com/getteacherdata.php");
        /* 
         * The above url is without any teacher id parameter which means retrieve all teachers data
         */

        /* Now we will execute the HttpGet request & store the response as HttpResponse Object
         */
        HttpResponse response = client.execute(get);

        /* Now we will take all the data from response as a string
         */
        String data = EntityUtils.toString(response.getEntity());

        // Response has data stored in the form of Entity which we retrieve by above method

        // Make sure you use org.json library strictly to use below constructor
        /* The string we got from http respnse
         * we passed it to create JSONArray
         */
        JSONArray jsonA = new JSONArray(data);

        JSONObject temp;
        int id;
        String name, address, qualification, contact;
        for (int i = 0; i < jsonA.length(); i++) {
            // Get array element as JSONObject
            temp = jsonA.getJSONObject(i);
            
            /* get data by using the column 
             * name of database as the key
             * and it will return the value pointing it
             */
            id = temp.getInt("id");
            name = temp.getString("name");
            address = temp.getString("address");
            qualification = temp.getString("qualification");
            contact = temp.getString("contact");
            System.out.println("ID: " + id);
            System.out.println("Name: " + name);
            System.out.println("Address: " + address);
            System.out.println("Qualification: " + qualification);
            System.out.println("Contact: " + contact);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68