Thursday, 28 September 2017

Java JSON

Parse JSON in Java


import org.json.*;


public class JsonTest {

public static void main(String args[]){
String str = "{ \"name\": \"Alice\", \"age\": 20 }";

JSONObject obj = new JSONObject(str);
String name= obj.getString("name");
int age = obj.getInt("age");
System.out.println("name: "+name);
System.out.println("age: "+age);

}
}

--------------------------------------------------------------------------------------------------------------


JSON Array


import org.json.*;

public class JSONArrayTest {

public static void main(String args[]){

String str = "{ \"number\": [3, 4, 5, 6] }";

JSONObject obj = new JSONObject(str);
JSONArray arr=obj.getJSONArray("number");

for(int i=0;i<arr.length();i++){
System.out.print(arr.getInt(i));
}

}
}


--------------------------------------------------------------------------------------------------------------
Google Maps API provides geocoding services to find the latitude/longitude of an address. The service returns results in JSON. The following method geocoding() does the following:
  • Build a URL to access the geocoding service.
  • Read from the URL.
  • Build a JSON object for the content.
  • Retrieve the first result from an array of results.
  • Print out the information.

Geo Coding


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;

import org.json.JSONObject;

public class GeoCoding {

public static void main(String args[]) throws IOException{

String addr ="1600+Amphitheatre+Parkway,+Mountain+View,+CA";
String s= "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";
s+=URLEncoder.encode(addr,"UTF-8");
URL url=new URL(s);

// read from the URL
    Scanner scan = new Scanner(url.openStream());
    String str = new String();
    while (scan.hasNext())
        str += scan.nextLine();
    scan.close();
 
    // build a JSON object
    JSONObject obj = new JSONObject(str);
    if (! obj.getString("status").equals("OK"))
        return;
 
    // get the first result
    JSONObject res = obj.getJSONArray("results").getJSONObject(0);
    System.out.println(res.getString("formatted_address"));
    JSONObject loc =
        res.getJSONObject("geometry").getJSONObject("location");
    System.out.println("lat: " + loc.getDouble("lat") +
                        ", lng: " + loc.getDouble("lng"));
}

}


--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------



http://mvnrepository.com/artifact/org.json/json/20160810



----------------------------------------------------------------------------------------

https://stackoverflow.com/questions/2591098/how-to-parse-json

http://theoryapp.com/parse-json-in-java/



Geocoding

Google Maps API provides geocoding services to find the latitude/longitude of an address. The service returns results in JSON. The following method geocoding() does the following:
  • Build a URL to access the geocoding service.
  • Read from the URL.
  • Build a JSON object for the content.
  • Retrieve the first result from an array of results.
  • Print out the information.
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
public static void geocoding(String addr) throws Exception
{
    // build a URL
                    "sensor=false&address=";
    s += URLEncoder.encode(addr, "UTF-8");
    URL url = new URL(s);
 
    // read from the URL
    Scanner scan = new Scanner(url.openStream());
    String str = new String();
    while (scan.hasNext())
        str += scan.nextLine();
    scan.close();
 
    // build a JSON object
    JSONObject obj = new JSONObject(str);
    if (! obj.getString("status").equals("OK"))
        return;
 
    // get the first result
    JSONObject res = obj.getJSONArray("results").getJSONObject(0);
    System.out.println(res.getString("formatted_address"));
    JSONObject loc =
        res.getJSONObject("geometry").getJSONObject("location");
    System.out.println("lat: " + loc.getDouble("lat") +
                        ", lng: " + loc.getDouble("lng"));
}


Example:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false


------------------------------------------------

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Google Building 42",
               "short_name" : "Google Bldg 42",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Google Bldg 42, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.42198310000001,
                  "lng" : -122.0853195
               },
               "southwest" : {
                  "lat" : 37.4214139,
                  "lng" : -122.0860042
               }
            },
            "location" : {
               "lat" : 37.4216548,
               "lng" : -122.0856374
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4230474802915,
                  "lng" : -122.0843128697085
               },
               "southwest" : {
                  "lat" : 37.4203495197085,
                  "lng" : -122.0870108302915
               }
            }
         },
         "place_id" : "ChIJPzxqWQK6j4AR3OFRJ6LMaKo",
         "types" : [ "premise" ]
      }
   ],
   "status" : "OK"
}


------------------------------------------------













































No comments:

Post a Comment