0

EDIT: I have another program, which uses direction service. It works. When I use that code in this geocode project, it throws the same error. (Eclipse, Java EE, Tomcat7)

for reference here's the request

http://maps.googleapis.com/maps/api/geocode/json?address=vancouver,bc&sensor=true

Here's there code

JSONObject obj = new JSONObject(s);
JSONObject res = obj.getJSONArray("results").getJSONObject(0);

String "s" is the return json. This works:

 System.out.println(obj.getJSONArray("results");

Though as soon as I try getJSONObject(0) I get the error. I was working with the directions service prior to this and it sends a very similar result.. which was working for me. Any advice is much appreciated! This has eaten a couple hours of my night now so I thought I'd seek some help.

5
  • Your code is working. getJSONArray("results").getJSONObject(0) is giving the result. Commented Dec 3, 2013 at 8:36
  • @Jhanvi Hmm. Any reason I'd be getting this particular error then? I just tried my old code for the directions service and it's giving me the same error now as well. Commented Dec 3, 2013 at 8:47
  • Can you write the json that you are parsing?? Is it the same link as mentioned in the question?? Commented Dec 3, 2013 at 9:01
  • The JSON return is identical. I'm using the same link as well. It's gotta be something wrong with the json package I'm thinking.. Commented Dec 3, 2013 at 9:12
  • Is this your import - import org.json.JSONObject; ?? Commented Dec 3, 2013 at 10:11

2 Answers 2

2
String lat = obj.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getString("lat");
Sign up to request clarification or add additional context in comments.

1 Comment

Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (Especially if it looks awfully similar to the original code.)
0

As you can see here:

{
   "results" : [
      {
         "address_components" : [
            {
               "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" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               **"lat"** : 37.4223329,
               **"lng"** : -122.0844192
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4236818802915,
                  "lng" : -122.0830702197085
               },
               "southwest" : {
                  "lat" : 37.4209839197085,
                  "lng" : -122.0857681802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

lat(Latitude) and lng(Longitude) are of type Double, hence the correct code (if you need latitude and longitude as Strings) is:

String latitude = Double.toString(jsonTree.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lat"));

String longitude = Double.toString(jsonTree.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng")); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.