It is currently 26 January 2020, 20:43 Advanced search

Google Map Parse JSON

Questions and answers on how to best use Instant Developer

Google Map Parse JSON

Postby rjberry » 25 October 2019, 16:23

Hi,
I am trying to parse json data returned from google maps api service. I am aware of the components but my budget cant afford all i need so need to try to write my own.
I got the json data back from the service but need to extract the Lat and Lon from the json data. Does anyone have any advice? Here is the json data:
thanks,
Rob Berry

{
"results" : [
{
"address_components" : [
{
"long_name" : "58",
"short_name" : "58",
"types" : [ "street_number" ]
},
{
"long_name" : "Harding Avenue",
"short_name" : "Harding Ave",
"types" : [ "route" ]
},
{
"long_name" : "Dover",
"short_name" : "Dover",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Morris County",
"short_name" : "Morris County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New Jersey",
"short_name" : "NJ",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "07801",
"short_name" : "07801",
"types" : [ "postal_code" ]
},
{
"long_name" : "4710",
"short_name" : "4710",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "58 Harding Ave, Dover, NJ 07801, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.8836204,
"lng" : -74.5424979
},
"southwest" : {
"lat" : 40.8833005,
"lng" : -74.54294729999999
}
},
"location" : {
"lat" : 40.883472,
"lng" : -74.5427122
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.8848094302915,
"lng" : -74.54137361970849
},
"southwest" : {
"lat" : 40.8821114697085,
"lng" : -74.54407158029152
}
}
},
"place_id" : "ChIJQUxaI2MKw4kReEmJEjiXtYA",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
rjberry
 
Posts: 20
Joined: 12 November 2013, 13:58

Re: Google Map Parse JSON

Postby lucabaldini » 28 October 2019, 7:31

I would try to use the new JSON library that you find in any 19.5 new project:

https://doc.instantdeveloper.com/eng/default.aspx?artid=4ba41099-4d31-407e-9a26-130b6c8b0b73&lang=eng

I see that the response is a JSON object so you would try something like that:
Code: Select all
   IDMap resp = JSON.parse([RESPONSE])
   float lat = resp.getValue("lat")
   float lng = resp.getValue("lng")


Check with the debug if it works fine...
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 3848
Joined: 1 October 2010, 17:03
Location: Bologna

Re: Google Map Parse JSON

Postby rjberry » 28 October 2019, 18:14

how should i store the response JSON data from the server? I am using an iframe in a static field.
Thanks,
Rob
rjberry
 
Posts: 20
Joined: 12 November 2013, 13:58

Re: Google Map Parse JSON

Postby lucabaldini » 29 October 2019, 7:19

I thought you were using the GetHTTP function to make that call:

https://doc.instantdeveloper.com/eng/default.aspx?artid=445761d2-0f80-47f1-a452-3adbfeb10ca8&lang=eng

There is no easy response for that... how are you making the HTTP call inside the iframe? What dou you mean with "I'm using an iframe in a stati field"? The iframe makes the http call? Where is the JSON? How did you get it?
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 3848
Joined: 1 October 2010, 17:03
Location: Bologna

Re: Google Map Parse JSON

Postby rjberry » 30 October 2019, 15:51

Hi,
I have been trying various ways to parse this data. I am friends with Jack wilson and am trying his class based method and have tried your new JSON library. Not having much success as of yet but one thing i am noticing is that when trying to process the json i posted in my first post, both methods of parsing seem to stop at the "results" node. I can not get it to proceed past that. what am i missing?
I am passing the string with the retrieved data to the JSON.parse() function to an object just as it shows in the sample code. I only get s key of "results" and the value of "status"
Please note this is a standard JSON coming from google mapping api. I have not changed anything.
Thanks for the help,
Rob Berry
rjberry
 
Posts: 20
Joined: 12 November 2013, 13:58

Re: Google Map Parse JSON

Postby lucabaldini » 31 October 2019, 7:46

Hi, I've done it :-)))

Let's start from the JSON. When I have to deal with JSON I need first to understand the exact structure... like you would normally do with XML. Every time you have to parse XML or JSON you have to understand where nodes are and how it's done. So for JSON I found this web site very useful: https://jsonlint.com/

There you can paste your JSON and it gets beautified:
json.png
json.png (85.19 KiB) Viewed 540 times


From that I can now understand how to read it:
- first I see that the JSON matches an object (everything starts with "{")... so that I have to use an IDMap as JSON.parse output. I've called it idm in my example
- the object contains a property called "result" that is an Array (you see the "["?). I've called it result in my example
- the first element of this result array is what I'm interested in... so, in my example, I've read the 0-th element... I've called it res0 and it's an object and I've to use an IDMap
- res0 contains an array called addess_components that I'm not interested in
- res0 contains a geometry object. That's I need to read because I want to read the location sub-sub-object (if I understood correctly what you are trying to do :-)))
- once I get the location object it's easy to extract both inner values: lat and lng.

Then I got the following:
debug.png
debug.png (12.43 KiB) Viewed 540 times


Here is my example project... I've used the exact JSON you have posted here.
JSONparse.zip
(452.33 KiB) Downloaded 36 times


I think that the difficult part here is understand the exact JSON's structure... once you have done that it's easy to read... you have have to create single statements (I call them "steps") to extract the branch you are interested in.

Hope it helps :-)
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 3848
Joined: 1 October 2010, 17:03
Location: Bologna


Return to Tips & Tricks

Who is online

Users browsing this forum: No registered users and 3 guests