Reading JSON with PeopleCode
PeopleCode, JSONBuilding on the example at Cedar Hills
I refactored it into a complex data type of Animal
, contained in an Array of Animal
:
class Animal
property string name;
property string species;
property boolean canBark;
property string likesFoods;
property string dislikesFoods;
end-class;
class Animals extends Array
method Animals(&jsonString_ As string);
method getByName(&name_ As string) Returns JSON_EXAMPLE:Animal;
...
Example JSON:
[
{
"name": "Meowsy",
"species" : "cat",
"canBark": false,
"foods": {
"likes": ["tuna", "catnip"],
"dislikes": ["ham", "zucchini"]
}
},
{
"name": "Barky",
"species" : "dog",
"canBark": true,
"foods": {
"likes": ["bones", "carrots"],
"dislikes": ["tuna"]
}
},
{
"name": "Purrpaws",
"species" : "cat",
"canBark": false,
"foods": {
"likes": ["mice"],
"dislikes": ["cookies"]
}
}
]
Example use:
Local JSON_EXAMPLE:Animals &Animals;
Local string &exampleJSON = GetHTMLText(HTML.EXAMPLE_JSON);
&Animals = create JSON_EXAMPLE:Animals(&exampleJSON);
Local any &Animal = &Animals.getByName("Barky");
Messagebox(0,"",0,0,"Barky is a " | &Animal.species);
Messagebox(0,"",0,0,"Barky likes to eat " | &Animal.likesFoods);