Android Question JSON parsing. I want only part of it...

Roger C

Active Member
Licensed User
Hello,

I have a json-string from which I want to extract everything under items 'StopLocation'.
There might be one item or it might be 100 items (in below example is 5) named StopLocation in the respons-string.

I've tried the utility Json Tree but it doesn't give me the code for StopLocation.

Could someone help me to just get the (in this case) 5 entries in the string below?
Maby place them in a map or something.
I need the values for id ,extId, name, lon, lat, weight, dist and products.
All as strings.

CoordLocation is of no interest.

Supplied are a picture from JsonTree with the parts that are interesting.

This is the string.
And thanks in advance. :)

B4X:
{"stopLocationOrCoordLocation":[{"CoordLocation":{"LocationNotes":{"LocationNote":[{"value":"4653985564","key":"PI","type":"I"},{"value":"amenity","key":"PC","type":"I"},{"value":"school","key":"PT","type":"I"},{"value":"Stockholm","key":"CN","type":"I"}]},"id":"A=4@O=Internationella Engelska Skolan@X=18005447@Y=59280893@u=0@U=176@L=980001513@","name":"Internationella Engelska Skolan","type":"POI","lon":18.005447,"lat":59.280893,"dist":65}},{"StopLocation":{"id":"A=1@O=Älvsjögården@X=18003991@Y=59280048@u=0@U=74@L=400113433@","extId":"400113433","name":"Älvsjögården","lon":18.003991,"lat":59.280048,"weight":1501,"dist":66,"products":8}},{"StopLocation":{"id":"A=1@O=Älvsjövägen@X=18002957@Y=59278969@u=0@U=74@L=400113440@","extId":"400113440","name":"Älvsjövägen","lon":18.002957,"lat":59.278969,"weight":384,"dist":199,"products":8}},{"CoordLocation":{"LocationNotes":{"LocationNote":[{"value":"219876895","key":"PI","type":"I"},{"value":"leisure","key":"PC","type":"I"},{"value":"park","key":"PT","type":"I"},{"value":"Stockholm","key":"CN","type":"I"}]},"id":"A=4@O=Lerkrogsparken@X=18007560@Y=59279607@u=0@U=176@L=980000611@","name":"Lerkrogsparken","type":"POI","lon":18.00756,"lat":59.279607,"dist":208}},{"CoordLocation":{"LocationNotes":{"LocationNote":[{"value":"30269298","key":"PI","type":"I"},{"value":"leisure","key":"PC","type":"I"},{"value":"park","key":"PT","type":"I"},{"value":"Stockholm","key":"CN","type":"I"}]},"id":"A=4@O=Prästgårdsparken@X=18009493@Y=59281855@u=0@U=176@L=980000985@","name":"Prästgårdsparken","type":"POI","lon":18.009493,"lat":59.281855,"dist":318}},{"StopLocation":{"id":"A=1@O=Skyttes plan@X=18001861@Y=59278043@u=0@U=74@L=400113828@","extId":"400113828","name":"Skyttes plan","lon":18.001861,"lat":59.278043,"weight":1501,"dist":319,"products":8}},{"CoordLocation":{"LocationNotes":{"LocationNote":[{"value":"1439606372","key":"PI","type":"I"},{"value":"amenity","key":"PC","type":"I"},{"value":"library","key":"PT","type":"I"},{"value":"Stockholm","key":"CN","type":"I"}]},"id":"A=4@O=Älvsjö bibliotek@X=18007830@Y=59278169@u=0@U=176@L=980000294@","name":"Älvsjö bibliotek","type":"POI","lon":18.00783,"lat":59.278169,"dist":331}},{"StopLocation":{"id":"A=1@O=Älvsjö station@X=18009897@Y=59278655@u=0@U=74@L=400113437@","extId":"400113437","name":"Älvsjö station","lon":18.009897,"lat":59.278655,"weight":15064,"dist":377,"products":9}},{"StopLocation":{"id":"A=1@O=Långbrodalsvägen@X=17999622@Y=59278178@u=0@U=74@L=400113830@","extId":"400113830","name":"Långbrodalsvägen","lon":17.999622,"lat":59.278178,"weight":384,"dist":384,"products":8}},{"CoordLocation":{"LocationNotes":{"LocationNote":[{"value":"210872321","key":"PI","type":"I"},{"value":"leisure","key":"PC","type":"I"},{"value":"park","key":"PT","type":"I"},{"value":"Stockholm","key":"CN","type":"I"}]},"id":"A=4@O=Viktoriaparken@X=17997771@Y=59279868@u=0@U=176@L=980000588@","name":"Viktoriaparken","type":"POI","lon":17.997771,"lat":59.279868,"dist":388}}],"serverVersion":"1.2","dialectVersion":"1.23","requestId":"1549914103639"}
 

Attachments

  • Image1.jpg
    Image1.jpg
    103.8 KB · Views: 69

Erel

Administrator
Staff member
Licensed User
B4X:
Dim s As String = $"{"stopLocationOrCoordLocation":..."$
Dim parser As JSONParser
parser.Initialize(s)
Dim root As Map = parser.NextObject
Dim stopLocationOrCoordLocation As List = root.Get("stopLocationOrCoordLocation")
For Each colstopLocationOrCoordLocation As Map In stopLocationOrCoordLocation
   If colstopLocationOrCoordLocation.ContainsKey("StopLocation") Then
       Dim stopLocation As Map = colstopLocationOrCoordLocation.Get("StopLocation")
       Log(stopLocation)
   End If
Next

I've tried the utility Json Tree but it doesn't give me the code for StopLocation.
The json tree tool assumes that the structure of arrays elements are the same. This is not the case here.
 
Upvote 0
Top