Android Question Help needed: Parsing GoogleMaps-API result with JSONParser

manuel-r

Member
Licensed User
Longtime User
Hi @all

I need some help with parsing the result of a GoogleMaps-API.
For example I call https://maps.googleapis.com/maps/api/geocode/json?latlng=49.74611,6.68916 and get some JSON as result:

B4X:
{
"results" : [
{
"address_components" : [
{
"long_name" : "2-4",
"short_name" : "2-4",
"types" : [ "street_number" ]
},
{
"long_name" : "Kamp",
"short_name" : "Kamp",
"types" : [ "route" ]
},
{
"long_name" : "Paderborn",
"short_name" : "Paderborn",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paderborn",
"short_name" : "PB",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Detmold",
"short_name" : "Detmold",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Nordrhein-Westfalen",
"short_name" : "NRW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
},
{
"long_name" : "33098",
"short_name" : "33098",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Kamp 2-4, 33098 Paderborn, Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.7167746,
"lng" : 8.7552165
},
"southwest" : {
"lat" : 51.7165506,
"lng" : 8.754006
}
},
"location" : {
"lat" : 51.7166888,
"lng" : 8.7541838
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 51.7180115802915,
"lng" : 8.755960230291501
},
"southwest" : {
"lat" : 51.7153136197085,
"lng" : 8.753262269708499
}
}
},
"place_id" : "EiZLYW1wIDItNCwgMzMwOTggUGFkZXJib3JuLCBEZXV0c2NobGFuZA",
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Kernstadt",
"short_name" : "Kernstadt",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Paderborn",
"short_name" : "Paderborn",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paderborn",
"short_name" : "PB",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Detmold",
"short_name" : "Detmold",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Nordrhein-Westfalen",
"short_name" : "NRW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Kernstadt, Paderborn, Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.7565651,
"lng" : 8.812093299999999
},
"southwest" : {
"lat" : 51.6835128,
"lng" : 8.717874799999999
}
},
"location" : {
"lat" : 51.7189199,
"lng" : 8.7575091
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.7565651,
"lng" : 8.812093299999999
},
"southwest" : {
"lat" : 51.6835128,
"lng" : 8.717874799999999
}
}
},
"place_id" : "ChIJ0dtCaZRMukcRZ_WRI7MRyFw",
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paderborn",
"short_name" : "Paderborn",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paderborn",
"short_name" : "PB",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Detmold",
"short_name" : "Detmold",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Nordrhein-Westfalen",
"short_name" : "NRW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paderborn, Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.8009578,
"lng" : 8.900563699999999
},
"southwest" : {
"lat" : 51.654255,
"lng" : 8.636301399999999
}
},
"location" : {
"lat" : 51.7189205,
"lng" : 8.757509299999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.8009578,
"lng" : 8.900563699999999
},
"southwest" : {
"lat" : 51.654255,
"lng" : 8.636301399999999
}
}
},
"place_id" : "ChIJ2x1hYZZMukcR7iLvWCqHzHM",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "33098",
"short_name" : "33098",
"types" : [ "postal_code" ]
},
{
"long_name" : "Paderborn",
"short_name" : "Paderborn",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paderborn",
"short_name" : "PB",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Detmold",
"short_name" : "Detmold",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Nordrhein-Westfalen",
"short_name" : "NRW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "33098 Paderborn, Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.7236131,
"lng" : 8.782977899999999
},
"southwest" : {
"lat" : 51.697342,
"lng" : 8.720092899999999
}
},
"location" : {
"lat" : 51.7113515,
"lng" : 8.7542577
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.7236131,
"lng" : 8.782977899999999
},
"southwest" : {
"lat" : 51.697342,
"lng" : 8.720092899999999
}
}
},
"place_id" : "ChIJh5E9l7tMukcRMHBbkYTyJxw",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Paderborn",
"short_name" : "PB",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Detmold",
"short_name" : "Detmold",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Nordrhein-Westfalen",
"short_name" : "NRW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paderborn, Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.87814590000001,
"lng" : 9.0173174
},
"southwest" : {
"lat" : 51.4473986,
"lng" : 8.402721099999999
}
},
"location" : {
"lat" : 51.61434269999999,
"lng" : 8.7027757
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.87814590000001,
"lng" : 9.0173174
},
"southwest" : {
"lat" : 51.4473986,
"lng" : 8.402721099999999
}
}
},
"place_id" : "ChIJsU_kEoS0u0cR6bGACN9_Ftc",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"address_components" : [
{
"long_name" : "Detmold",
"short_name" : "Detmold",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Nordrhein-Westfalen",
"short_name" : "NRW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Detmold, Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.53146959999999,
"lng" : 9.461634900000002
},
"southwest" : {
"lat" : 51.441563,
"lng" : 8.075595099999999
}
},
"location" : {
"lat" : 51.85526369999999,
"lng" : 8.8911421
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 52.53146959999999,
"lng" : 9.461634900000002
},
"southwest" : {
"lat" : 51.441563,
"lng" : 8.075595099999999
}
}
},
"place_id" : "ChIJEbi-0QNBukcRoy2sOKYLwzY",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Nordrhein-Westfalen",
"short_name" : "NRW",
"types" : [
"administrative_area_level_1",
"establishment",
"point_of_interest",
"political"
]
},
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Nordrhein-Westfalen, Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.53146959999999,
"lng" : 9.461634900000002
},
"southwest" : {
"lat" : 50.322701,
"lng" : 5.8663425
}
},
"location" : {
"lat" : 51.43323669999999,
"lng" : 7.661593799999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 52.53146959999999,
"lng" : 9.461634900000002
},
"southwest" : {
"lat" : 50.322701,
"lng" : 5.8663425
}
}
},
"place_id" : "ChIJoUhCjnwWuUcRX4_XT34yTC8",
"types" : [
"administrative_area_level_1",
"establishment",
"point_of_interest",
"political"
]
},
{
"address_components" : [
{
"long_name" : "Deutschland",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Deutschland",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 55.0815,
"lng" : 15.0418962
},
"southwest" : {
"lat" : 47.2701115,
"lng" : 5.8663425
}
},
"location" : {
"lat" : 51.165691,
"lng" : 10.451526
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 55.05812359999999,
"lng" : 15.0418487
},
"southwest" : {
"lat" : 47.2702482,
"lng" : 5.8664874
}
}
},
"place_id" : "ChIJa76xwh5ymkcRW-WRjmtd6HU",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
What I need is the value of "formatted_address" so in this example the result should be "Kamp 2-4, 33098 Paderborn, Deutschland".

Can anyone explain how can I do this with JSONParser?

Thx
Manuel
 

DonManfred

Expert
Licensed User
Longtime User
You know http://basic4ppc.com:51042/json/index.html ???
Go and check it if not ;)
Paste the son and the website will return a b4x code to parse it


This is the code for your json

B4X:
Dim parser As JSONParser
parser.Initialize(<text>) ' put your jsonstring here....
Dim root As Map = parser.NextObject
Dim results As List = root.Get("results")
For Each colresults As Map In results
Dim formatted_address As String = colresults.Get("formatted_address")
Dim types As List = colresults.Get("types")
For Each coltypes As String In types
Next
Dim geometry As Map = colresults.Get("geometry")
Dim viewport As Map = geometry.Get("viewport")
Dim southwest As Map = viewport.Get("southwest")
Dim lng As Double = southwest.Get("lng")
Dim lat As Double = southwest.Get("lat")
Dim northeast As Map = viewport.Get("northeast")
Dim lng As Double = northeast.Get("lng")
Dim lat As Double = northeast.Get("lat")
Dim bounds As Map = geometry.Get("bounds")
Dim southwest As Map = bounds.Get("southwest")
Dim lng As Double = southwest.Get("lng")
Dim lat As Double = southwest.Get("lat")
Dim northeast As Map = bounds.Get("northeast")
Dim lng As Double = northeast.Get("lng")
Dim lat As Double = northeast.Get("lat")
Dim location As Map = geometry.Get("location")
Dim lng As Double = location.Get("lng")
Dim lat As Double = location.Get("lat")
Dim location_type As String = geometry.Get("location_type")
Dim address_components As List = colresults.Get("address_components")
For Each coladdress_components As Map In address_components
  Dim types As List = coladdress_components.Get("types")
  For Each coltypes As String In types
  Next
  Dim short_name As String = coladdress_components.Get("short_name")
  Dim long_name As String = coladdress_components.Get("long_name")
Next
Dim place_id As String = colresults.Get("place_id")
Next
Dim status As String = root.Get("status")
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
@DonManfred was faster :)

Additionally: { represents a Map in B4x and [ a list. For me this was a good help to read a JSON structure. So all starts with a map as you can see because the data begins with a {. The key of the map is "address_components" and it's value is a list. The list (starts with [) itsself carries some maps and so on.

Grüße nach Paderborn
 
Upvote 0
Top