Android Question How can I make the json as map object

Alphaw

Member
Licensed User
Longtime User
Hello everyone,

I have read the post in https://www.b4x.com/android/forum/threads/android-json-tutorial.6923/
The post how how to get the element in the json, however, if I need to find some element within several array and object, how can I do so?

B4X:
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "香港",
               "short_name" : "HK",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "香港",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 22.561968,
                  "lng" : 114.4069573
               },
               "southwest" : {
                  "lat" : 22.1533884,
                  "lng" : 113.835078
               }
            },
            "location" : {
               "lat" : 22.396428,
               "lng" : 114.109497
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 22.561968,
                  "lng" : 114.4069573
               },
               "southwest" : {
                  "lat" : 22.1533884,
                  "lng" : 113.835079
               }
            }
         },
         "place_id" : "ChIJD5gyo-3iAzQRfMnq27qzivA",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

Above json is what I receive in google map geocode api, actually we may want to use the date at:
"geometry"-->"location"--> both "lat" and "lng". But I don't know to get those elements with map and list function. Please help. Thank you.

Alpha
 

DonManfred

Expert
Licensed User
Longtime User
Above json is what I receive in google map geocode api, actually we may want to use the date at:
"geometry"-->"location"--> both "lat" and "lng". But I don't know to get those elements with map and list function. Please help. Thank you.

1. I´used http://basic4ppc.com:51042/json/index.html and post your jsonstring there

This is the code it returns.
B4X:
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")

For your needs you can strip it down to i think

B4X:
Dim results As List = root.Get("results")
For Each colresults As Map In results
  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")
Next
Dim status As String = root.Get("status")

Hope it helps
 
Upvote 0

Descartex

Well-Known Member
Licensed User
Longtime User
The Json parser tool is a work of art ;)
 
Upvote 0

Alphaw

Member
Licensed User
Longtime User
Yes, I use the json parser tool, it is too powerful!!!!
Thank you a lot. It save my time from now on.
 
Upvote 0

Similar Threads

Top