1. Hannu Pikkarainen
  2. PowerBuilder
  3. Thursday, 5 April 2018 08:50 AM UTC

Hi,

Could someone advice me how to access JSON-data in deeper levels, for example street and area in this example data. I'm able to read upper level like location and formatted_address but how to access child data of address_components?

{

"results": [

{

"location": "POINT (24.937573 60.160291)",

"address_components": {

"street": "Merimiehenkatu",

"area": "Uusimaa",

"country": "Finland",

"street_nbr": "29",

"post_office": "Helsinki",

"postal_code": "00150",

"country_code": "FI"

},

"formatted_address": "Merimiehenkatu 29, Helsinki, 00150 Uusimaa, Finland",

"location_type": "POINT"

},

{

"location": "POINT (-79.906981 42.991202)",

"address_components": {

"street": "Fourth Line",

"area": "Ontario",

"country": "Canada",

"street_nbr": "00150",

"post_office": "Byng",

"postal_code": "N3W 2G2",

"country_code": "CA"

},

"formatted_address": "00150 Fourth Line, Byng, Ontario N3W 2G2, Canada",

"location_type": "POINT"

},

{

"location": "POINT (21.483384 61.140578)",

"address_components": {

"street": "Merimiehenkatu",

"area": "Satakunta",

"country": "Finland",

"street_nbr": "29",

"post_office": "Rauma",

"postal_code": "26100",

"country_code": "FI"

},

"formatted_address": "Merimiehenkatu 29, Rauma, 26100 Satakunta, Finland",

"location_type": "POINT"

},

{

"location": "POINT (22.954244 59.836179)",

"address_components": {

"street": "Merimiehenkatu",

"area": "Uusimaa",

"country": "Finland",

"street_nbr": "29",

"post_office": "Hanko",

"postal_code": "10900",

"country_code": "FI"

},

"formatted_address": "Merimiehenkatu 29, Hanko, 10900 Uusimaa, Finland",

"location_type": "POINT"

},

{

"location": "POINT (21.401145 60.802559)",

"address_components": {

"street": "Merimiehenkatu",

"area": "Varsinais-Suomi",

"country": "Finland",

"street_nbr": "29",

"post_office": "Uusikaupunki",

"postal_code": "23500",

"country_code": "FI"

},

"formatted_address": "Merimiehenkatu 29, Uusikaupunki, 23500 Varsinais-Suomi, Finland",

"location_type": "POINT"

}

]

}

BR, Hannu

Accepted Answer
Bruce Armstrong Accepted Answer Pending Moderation
  1. Thursday, 5 April 2018 17:44 PM UTC
  2. PowerBuilder
  3. # Permalink
 string          ls_pathname, ls_filename, ls_key, ls_key2, ls_value, ls_key3  
 long          ll_root, ll_index, ll_count, ll_level1, ll_index2, ll_count2, ll_child, ll_count3, ll_index3, ll_child2, ll_child3, ll_count4, ll_index4  
 JSONParser jp  
 JsonItemType it, it2  
 jp = CREATE JSONParser  
 GetFileOpenName ( "Select JSON file", ls_pathname, ls_filename )  
 jp.LoadFile ( ls_pathname )  
 ll_root = jp.GetRootItem()  
 ll_count = jp.GetChildCount(ll_root)  
 for ll_index = 1 to ll_count  
      ls_key = jp.GetChildKey(ll_root, ll_index)  
      ll_level1 = jp.GetItemArray ( ll_root, ls_key )  
      ll_count2 = jp.GetChildCount(ll_level1)  
      FOR ll_index2 = 1 to ll_count2  
           ll_child = jp.GetChildItem ( ll_level1, ll_index2 )  
           ll_count3 = jp.GetChildCount(ll_child)  
           FOR ll_index3 = 1 TO ll_count3  
                ll_child2 = jp.GetChildItem ( ll_child, ll_index3 )  
                ls_key2 = jp.GetChildKey ( ll_child, ll_index3 )  
                it = jp.GetItemType ( ll_child2 )  
                CHOOSE CASE it  
                     CASE JsonStringItem!  
                          ls_value = jp.GetItemString ( ll_child2 )  
                          MessageBox ( ls_key2, ls_value )  
                     CASE JsonNumberItem!  
                          CONTINUE  
                     CASE JsonBooleanItem!  
                          CONTINUE  
                     CASE JsonNullItem!  
                          CONTINUE  
                     CASE JsonObjectItem!  
                          ll_count4 = jp.GetChildCount(ll_child2)  
                          FOR ll_index4 = 1 TO ll_count4  
                               ll_child3 = jp.GetChildItem ( ll_child2, ll_index4 )  
                               ls_key3 = jp.GetChildKey ( ll_child2, ll_index4 )  
                               it2 = jp.GetItemType ( ll_child3 )  
                               CHOOSE CASE it2  
                                    CASE JsonStringItem!  
                                         ls_value = jp.GetItemString ( ll_child3 )  
                                         MessageBox ( ls_key2 + ' - ' + ls_key3, ls_value )  
                                    CASE JsonNumberItem!  
                                         CONTINUE  
                                    CASE JsonBooleanItem!  
                                         CONTINUE  
                                    CASE JsonNullItem!  
                                         CONTINUE  
                                    CASE JsonObjectItem!  
                                         CONTINUE  
                                    CASE JsonArrayItem!  
                                         CONTINUE  
                               END CHOOSE  
                          NEXT  
                     CASE JsonArrayItem!  
                          CONTINUE  
                END CHOOSE  
           NEXT  
      NEXT  
 next  
Comment
  1. Hannu Pikkarainen
  2. Friday, 6 April 2018 10:23 AM UTC
Thanks Bruce,



Exactly what I was looking for.



BR, Hannu

  1. Helpful
There are no comments made yet.


There are replies in this question but you are not allowed to view the replies from this question.