1. Andrew Davis
  2. PowerBuilder
  3. Sunday, 10 June 2018 12:23 PM UTC
I hope you can help
I have the following code which works fine for the first level of fields, how do i get the data for the address1 address2 city and postcode
 
// json return string is 
{
  "id": "W2DBZqIkce",
  "status": "active",
  "type": "customer",
  "ref": "",
  "firstname": "Bob",
  "lastname": "Baker", 
"email": "bbaker@myemail",
  "mobile": "012345678",
  "address": {
    "address1": "9 My Address",
    "address2": "Town",
    "city": "London",
    "postcode": "W1 3PA"
  },
  "dateCreated": "2018-02-21 15:02:57"
}
 
// powerbuilder code
 
 
JSONParser json
json = create JSONParser
ls_result = json.loadstring( ls_string )
 
ll_root = json.getrootitem( )
 
ls_id = json.getitemstring( ll_root, "id" )
ls_status = json.getitemstring( ll_root, "status" )
ls_type = json.getitemstring( ll_root, "type" )
ls_ref = json.getitemstring( ll_root, "ref")
ls_firstname = json.getitemstring( ll_root, "firstname")
ls_lastname = json.getitemstring( ll_root, "lastname")
ls_email = json.getitemstring( ll_root, "email")
ls_mobile = json.getitemstring( ll_root, "mobile")
 
thanks in advance as always 
 
Andrew
Accepted Answer
David Edison Accepted Answer Pending Moderation
  1. Monday, 11 June 2018 02:46 AM UTC
  2. PowerBuilder
  3. # Permalink

You can just add something like this at the end of your code:

long ll_address
string ls_address1, ls_address2

ll_address=json.GetItemObject(ll_root,"address")
ls_address1=json.getItemString(ll_address,"address1")
ls_address2=json.getItemString(ll_address,"address2")

Regards,

Tom Jiang

Comment
  1. Andrew Davis
  2. Monday, 11 June 2018 09:39 AM UTC
Awesome - worked perfectly

  1. Helpful
There are no comments made yet.
Ludwin Feiten Accepted Answer Pending Moderation
  1. Tuesday, 8 November 2022 15:16 PM UTC
  2. PowerBuilder
  3. # 1

Or you user the ItemPath:
ls_address1=json.getitemstring( "/address/address1")

Comment
  1. Kevin Ridley
  2. Wednesday, 9 November 2022 14:24 PM UTC
That's nice! Thanks for posting, didn't even know we could do that for nested values. That can really come in handy, especially processing in a loop.
  1. Helpful
  1. Benjamin Gaesslein
  2. Thursday, 10 November 2022 07:02 AM UTC
TIL that this is an option, thanks.
  1. Helpful 1
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.