1

Here is the json code..i am new to json parsing..so please let me know how to parse this json data to java. thanks in advance!!

  {
     "geometry" : {
        "location" : {
           "lat" : 13.0561990,
           "lng" : 80.2348820
        }
     },
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
     "id" : "cf67cea9c203824f58eaf5abe98a08912593bf90",
     "name" : "Mahalingapuram Branch",
     "reference" : "CnRsAAAAlAu76wP4EspUn8qk6yUBndsDI7RaEmDaXHc2gP1bomAP_22ZeswWfj3ganEwX9jO2BYjRo6eoeKlwrSeMNDv5h94zqnsNncDntlKN_MqC-gegTBZLX5YxOSA-vzuk6bKe3BJlHytW_wKJJs0Nxf3jRIQT1yYWTlDdMhk9EyS8ulMghoUZK5J51RRVKo12LPdHm_qCQWX_VA",
     "types" : [ "establishment" ],
     "vicinity" : "Mahalingapuram Main Road, Nungambakkam, Chennai"
  }

after parsing the java code here but error occur

public static void main(String[] args) throws Exception{
    InputStream is = JsonParsing.class.getResourceAsStream( "");
    String jsonTxt = IOUtils.toString( is );
    JSONObject json = (JSONObject)JSONSerializer.toJSON(jsonTxt);   
    JSONObject html_attribution = json.getJSONObject(“html_attribution");
    JSONObject geometry= json.getJSONObject(“geometry”);
    JSONObject location= json.getJSONObject(“location”);

    double latitude = json.getDouble("latitude");
    double longitude = json.getDouble("longitude");
    String icon= result.getString("icon");
    String id = result.getString("id");
    String reference= result.getString("reference");
    String name = result.getString("name");
    String vicinity = result.getstring("vicinity");

    System.out.println("html_attribution: " + html_attribution);
    System.out.println("location: " + location);
    System.out.println(“latitude: “ + latitude);
    System.out.println(“latitude: “ + longitude);
    System.out.println("result: " + result);
}
kds
  • 28,155
  • 9
  • 38
  • 55
murali
  • 119
  • 1
  • 3
  • 7
  • 1
    possible duplicate of [Parse JSON with org.json](http://stackoverflow.com/questions/6442347/parse-json-with-org-json) – bzlm Sep 30 '11 at 07:19

4 Answers4

2

I use the following:

http://code.google.com/p/google-gson/

Serialize to JSON:

Gson gson = new GsonBuilder().create(); 
String jsonOutput = gson.toJson(someObject);

De-serialize from JSON:

BagOfPrimitives obj = gson.fromJson(json, BagOfPrimitives.class);

A user guide can be found here:

https://sites.google.com/site/gson/gson-user-guide

Bradley Stacey
  • 161
  • 1
  • 8
1

Use a java library from http://www.json.org/.

flash
  • 6,730
  • 7
  • 46
  • 70
1

Take a look at the GSON Google project. Its a library that lets you get all your JSON data into Java objects. Its quite simple.

ScratchMyTail
  • 281
  • 3
  • 10
0
JSONObject geometry = json.getJSONObject("geometry");

JSONObject location = json.getJSONObject("location");

String lat = location.getString("lat");
String lng = location.getString("lng");

icon = json.getString("icon");

id = json.getString("id");

name = json.getString("name");

reference = json.getString("reference");

vicinity = json.getString("vicinity");

JSOnArray types = json.getJSONArray("types");

for (int i=0;i<=types.length();i++)
{

JSonObject temJson = (JSONObject) types.get(i);

String Varialb = temJson.get(i);

}
animuson
  • 53,861
  • 28
  • 137
  • 147
kirti
  • 183
  • 16