Parse JSONItem in Array

I’m working on an internal project where I’m sending an address to Google and getting the Longitude and Latitude returned.

I’m able to get the JSON response back and I"m also able to access one other value in the JSON but for some reason I’m really struggling getting further into the array.

Here is the code I’m using:

[code] dim s as new HttpSocket
dim fullUrl as string
dim stringContent as string
dim jsonContent as new JSONItem()
dim stringResponse as string

dim timeoutSeconds as integer = 10
dim responseCode as integer
Dim se() as string

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

s.SetRequestContent(stringContent, “application/json”)
stringResponse = s.Get(fullUrl, timeoutSeconds)

dim response as new JSONItem(StringResponse)
Dim result as JSONItem = response.Value(“results”)

Dim i as integer
Dim addcompitem as JSONItem
for i = 0 to result.count - 1
addcompitem = result.Child(i)
se.append addcompitem.Value(“formatted_address”)
next
[/code]

This gives me a JSON return that looks like this:

{ "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" : "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 Parkway, Mountain View, CA 94043, USA", "geometry" : { "location" : { "lat" : 37.4219985, "lng" : -122.0839544 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 37.4233474802915, "lng" : -122.0826054197085 }, "southwest" : { "lat" : 37.4206495197085, "lng" : -122.0853033802915 } } }, "types" : [ "street_address" ] } ], "status" : "OK" }

What I’m trying to access are the lat and lng values under location which is under geometry. Any assistance on stepping down though this? Per my code above I can get the value from formatted_address with no problems but anything else I’ve tried to get to geometry and location I’m always getting keynofound error.

I get how to create my own JSON data by reviewing the JSON Example and even access some of the data further down. I’m hoping it is just something simple I’m missing.

Thanks.

Something like

dim response as new JSONItem(StringResponse)
Dim result as JSONItem = response.Value("results")
dim geometry as JSONItem = result.Child("geometry")
dim location as JSONItem = geometry.Value("location")
dim lat as string = location.Value("lat")
dim lng as string = location.Value("lng")

Maybe better:
dim response as new JSONItem(StringResponse)
Dim result as JSONItem = response.Value(“results”)
dim currentResult as JSONItem=result.Value(0)
dim geometry as JSONItem = currentResult.Child(“geometry”)
dim location as JSONItem = geometry.Value(“location”)
dim lat as string = location.Value(“lat”)
dim lng as string = location.Value(“lng”)

Antonio,
I marked your post as answer. Works great for what I need.
Thanks to Tim as well!