0

I'm using Google's Geocoding API to get a JSON string containing geocode location information. Here's the string I am getting back from Google.

{
  "status": "OK",
  "results": [ {
"types": [ "street_address" ],
"formatted_address": "550 Susong Dr, Morristown, TN 37814, USA",
"address_components": [ {
  "long_name": "550",
  "short_name": "550",
  "types": [ "street_number" ]
}, {
  "long_name": "Susong Dr",
  "short_name": "Susong Dr",
  "types": [ "route" ]
}, {
  "long_name": "Morristown",
  "short_name": "Morristown",
  "types": [ "locality", "political" ]
}, {
  "long_name": "Morristown",
  "short_name": "Morristown",
  "types": [ "administrative_area_level_3", "political" ]
}, {
  "long_name": "Hamblen",
  "short_name": "Hamblen",
  "types": [ "administrative_area_level_2", "political" ]
}, {
  "long_name": "Tennessee",
  "short_name": "TN",
  "types": [ "administrative_area_level_1", "political" ]
}, {
  "long_name": "United States",
  "short_name": "US",
  "types": [ "country", "political" ]
}, {
  "long_name": "37814",
  "short_name": "37814",
  "types": [ "postal_code" ]
} ],
"geometry": {
  "location": {
    "lat": 36.2422740,
    "lng": -83.3219410
  },
  "location_type": "ROOFTOP",
  "viewport": {
    "southwest": {
      "lat": 36.2391264,
      "lng": -83.3250886
    },
    "northeast": {
      "lat": 36.2454216,
      "lng": -83.3187934
    }
  }
}

} ] }

However, when I run the following code in Java I get a "java.lang.ClassCastException: java.lang.String incompatible with net.sf.json.JSONObject" error.

  URL url = new URL(URL + "&address=" + URLEncoder.encode(address, "UTF-8") + "&signature=" + key);
  URLConnection conn = url.openConnection();
  ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
  IOUtils.copy(conn.getInputStream(), output);
  output.close();

  GAddress gaddr = new GAddress();
  JSONObject json = JSONObject.fromObject(output.toString());
  JSONObject placemark = (JSONObject) query(json, "Placemark[0]");

I'm not sure why I'm getting the error. The Google response looks like a valid JSON string to me. Anyone else had problems with this? I'm open to using something besides the net.sf.json if it doesn't play nice with Google for some reason.

Thanks,

Andrew

4
  • Does query(json, "Placemark[0]") return a String? Commented Dec 30, 2010 at 15:56
  • When I put "System.out.println(query(json, "Placemark[0]").getClass());" before the last line it says it is returning a java.lang.String. Commented Dec 30, 2010 at 16:44
  • ) so now you know what to do. Either return JSONObject from query() or remove the Casting and assignment. Commented Dec 30, 2010 at 16:57
  • See comments below. Thanks Nishant. Commented Dec 30, 2010 at 20:19

1 Answer 1

1

It looks like you are getting a string back from your original function call. To be sure, you could add

System.out.println(query(json, "Placemark[0]").class);

right before the last line. This will give you the type of the object you are dealing with.

Sign up to request clarification or add additional context in comments.

2 Comments

It says it is a java.lang.String.
Apparently the code I was looking at was using an older version of the response from Google and it was getting unexpected results. I took the response from Google and able to figure out where the type mismatches were occurring. Thanks, guys.

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.