Testing PB right now, love it! I'm parsing a JSON successfully but have trouble on understanding how to handle nested objects. I'm using a simple dummy json for testing:
Code: Select all
[
{
"_id": "5a16ed1b873eb495e083fd29",
"index": 0,
"guid": "9f54a017-50bf-47c0-a3e1-0710154b7f04",
"isActive": true,
"balance": "$2,953.75",
"picture": "http://placehold.it/32x32",
"age": 31,
"eyeColor": "brown",
"name": "Leonor Blevins",
"gender": "female",
"company": "ERSUM",
"email": "leonorblevins@ersum.com",
"phone": "+1 (850) 456-2539",
"address": "905 Lenox Road, Disautel, Delaware, 4197",
"about": "Ipsum ut magna id in. blah blah blah.\r\n",
"registered": "2016-04-17T06:27:14 -02:00",
"latitude": -56.411933,
"longitude": -25.603861,
"tags": [
"reprehenderit",
"quis",
"commodo",
"consequat",
"enim",
"velit",
"tempor"
],
"friends": [
{
"id": 0,
"name": "Bertha Matthews"
},
{
"id": 1,
"name": "Henry Glover"
},
{
"id": 2,
"name": "Frye Bruce"
}
],
"greeting": "Hello, Leonor Blevins! You have 2 unread messages.",
"favoriteFruit": "banana"
}
]
Code: Select all
;# Structure
Structure Example
_id.s
index.l
guid.s
isActive.l
balance.s
picture.s
age.l
eyeColor.s
name.s
gender.s
company.s
email.s
phone.s
address.s
about.s
registered.s
latitude.l
longitude.l
List tags.s()
;TODO friends
greeting.s
favoriteFruit.s
EndStructure
;# Enumeration
Enumeration
#J ;JSON File
EndEnumeration
;# Globals
Global NewList Example.Example()
;# Load JSON
If LoadJSON(#J, "example-simple.json")
Debug "example.json loaded"
ExtractJSONList(JSONValue(#J), Example())
Else
Debug "Can not load example.json"
EndIf
;Debug
Debug ""
Debug "---"
Debug ""
ForEach Example()
Debug "_id.l: " + Example()\_id
Debug "guid.s: " + Example()\guid
Debug "isActive.l: " + Example()\isActive
Debug "balance.s: " + Example()\balance
Debug "picture.s: " + Example()\picture
Debug "age.l: " + Example()\age
Debug "eyeColor.s: " + Example()\eyeColor
Debug "name.s: " + Example()\name
Debug "gender.s: " + Example()\gender
Debug "company.s: " + Example()\company
Debug "email.s: " + Example()\company
Debug "phone.s" + Example()\phone
Debug "address.s: " + Example()\address
Debug "about.s: " + Example()\about
Debug "registered.s: " + Example()\registered
Debug "latitude.l: " + Example()\latitude
Debug "longitude.l: " + Example()\longitude
;List tags()
ForEach Example()\tags()
Debug Chr(9) + "tags: " + Example()\tags()
Next
;List friends()
;TODO
Debug "greeting.s: " + Example()\greeting
Debug "favoriteFruit.s: " + Example()\favoriteFruit
Debug ""
Debug "---"
Debug ""
Next
How do you guys parse JSON Objects inside another JSON Object is the question basically.
Very impressed on how quick I can prototype stuff in PB. Looking forward to learn more!
Thanks for reading :)