Press enter to see results or esc to cancel.


How to Parse Nested JSON using JAVA

In this video I have shown how to Parse nested JSON response array using JAVA.
Using two real http request example,
1. ipinfodb.com: This website will give JSON response of IP location details for the HTTP request we give.
2. google map api : Google Map API will give JSON response of the Location details for the GEO coordinates http request.

To read json Response you will have to add java-jason.jar to class path.

  1. Project Structure,

    
    
  2. HTTP Request send to ipinfodb.com after registering to get the API Key,

    http://api.ipinfodb.com/v3/ip-city/?key=9d64fcfdfacc213c7ddf4ef911dfe97b55e4696bee3532bf830276c09ebad06b&ip=74.125.45.100&format=json
    

    JSON Response through SITE,

    
    
    {
    	"statusCode" : "OK",
    	"statusMessage" : "",
    	"ipAddress" : "74.125.45.100",
    	"countryCode" : "US",
    	"countryName" : "United States",
    	"regionName" : "Oklahoma",
    	"cityName" : "Tulsa",
    	"zipCode" : "74101",
    	"latitude" : "36.154",
    	"longitude" : "-95.9928",
    	"timeZone" : "-05:00"
    }
    
  3. Get_Location_From_IP.java

    
    package com.chillyfacts.com;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import org.json.JSONObject;
    public class Get_Location_From_IP {
        public static void main(String[] args) {
            try {
                String ip = "74.125.45.100";
                String key = "9d64fcfdfacc213csfsfc7ddsf4ef911dfe97b55e4fdsf696be3532bf8302876c09ebad06b";
                String url = "http://api.ipinfodb.com/v3/ip-city/?key=" + key + "&ip=" + ip + "&format=json";
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("User-Agent", "Mozilla/5.0");
                BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in .readLine()) != null) {
                    response.append(inputLine);
                } in .close();
                System.out.println(response.toString());
                JSONObject obj_JSONObject = new JSONObject(response.toString());
                System.out.println("result after Reading JSON Response");
                System.out.println("statusCode- " + obj_JSONObject.getString("statusCode"));
                System.out.println("statusMessage- " + obj_JSONObject.getString("statusMessage"));
                System.out.println("ipAddress- " + obj_JSONObject.getString("ipAddress"));
                System.out.println("countryCode- " + obj_JSONObject.getString("countryCode"));
                System.out.println("countryName- " + obj_JSONObject.getString("countryName"));
                System.out.println("regionName- " + obj_JSONObject.getString("regionName"));
                System.out.println("cityName- " + obj_JSONObject.getString("cityName"));
                System.out.println("zipCode- " + obj_JSONObject.getString("zipCode"));
                System.out.println("latitude- " + obj_JSONObject.getString("latitude"));
                System.out.println("longitude- " + obj_JSONObject.getString("longitude"));
                System.out.println("timeZone- " + obj_JSONObject.getString("timeZone"));
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
    

    Out put response after parsing the JSON Response,

    {	"statusCode" : "OK",	"statusMessage" : "",	"ipAddress" : "74.125.45.100",	"countryCode" : "US",	"countryName" : "United States",	"regionName" : "Oklahoma",	"cityName" : "Tulsa",	"zipCode" : "74101",	"latitude" : "36.154",	"longitude" : "-95.9928",	"timeZone" : "-05:00"}
    result after Reading JSON Response
    statusCode- OK
    statusMessage- 
    ipAddress- 74.125.45.100
    countryCode- US
    countryName- United States
    regionName- Oklahoma
    cityName- Tulsa
    zipCode- 74101
    latitude- 36.154
    longitude- -95.9928
    timeZone- -05:00
    
  4. Register in Below link to get the API key for testing Get Coordinate location through google map api.
    https://developers.google.com/maps/documentation/geocoding/start

    HTTP Request we passed to get the response is,

    https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=AIzaSyAE6FolYnePocr26o9spd7ZytyGVcM29jHk
    

    JSON Response for the HTTP request

  5. Get_Cordinate_location.java
    
    package com.chillyfacts.com;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import org.json.JSONArray;
    import org.json.JSONObject;
    public class Get_Cordinate_location {
        public static void main(String[] args) {
            try {
                String key = "AIzaSyAE6FolYntrWscr26o9pd7ZytyGVcM29jHk";
                String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=" + key;
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                // optional default is GET
                con.setRequestMethod("GET");
                //add request header
                con.setRequestProperty("User-Agent", "Mozilla/5.0");
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in .readLine()) != null) {
                    response.append(inputLine);
                } in .close();
                //  System.out.println(response.toString());
                JSONObject obj_JSONObject = new JSONObject(response.toString());
                JSONArray obj_JSONArray = obj_JSONObject.getJSONArray("results");
                System.out.println(obj_JSONArray);
                System.out.println("--" + obj_JSONArray.length());
                JSONObject obj_JSONObject2 = obj_JSONArray.getJSONObject(5);
                System.out.println("--element 5---");
                System.out.println(obj_JSONObject2);
                JSONArray address_components_array = obj_JSONObject2.getJSONArray("address_components");
                JSONObject obj_JSONObject3 = address_components_array.getJSONObject(0);
                System.out.println("long_name-" + obj_JSONObject3.getString("long_name"));
                System.out.println("short_name-" + obj_JSONObject3.getString("short_name"));
                System.out.println("formatted_address-" + obj_JSONObject2.getString("formatted_address"));
                System.out.println("formatted_address-" + obj_JSONObject2.getString("place_id"));
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
    

    The response after parsing the JSON response,

    [{"formatted_address":"277 Bedford Ave, Brooklyn, NY 11211, USA","types":["street...
    --10
    --element 5---
    {"formatted_address":"Brooklyn, NY 11211, USA","types":["postal_code"],"geometry".....
    long_name-11211
    short_name-11211
    formatted_address-Brooklyn, NY 11211, USA
    formatted_address-ChIJvbEjlVdZwokR4KapM3WCFRw