JSON and var [solved]


I’m trying to get my head around the var class by playing with JSON. It is easy enough to parse the JSON to a var, but I am finding it impossible to inspect the data in a meaningful way. Taking the wikipedia JSON example

{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }

How would I go about doing something simple like extracting the “age”? I’m sure that once I get started it will all fall into place. The concepts are just a little to abstract for me to grasp from the Doxygen generated var documentation.


Ignore this - I think I’m starting to get somewhere after finding the wiki pages . .



OK. I have hit a bump. I need to extract an array of identifiers from a var that is an object. This is so I can make a JSON validator given a schema, but I can’t find a method to do this. HELP :?


I look up specific keys, eg v[“firstName”] will give “John”. If you want to get all the keys, is the dynamic object actually a PropertySet. Maybe you can get the identifiers from that.


Thanks for the response Hugh. I have worked out how to extract data from specific key identities, but the var documentation has no methods that return a propertySet or list of identifiers (as far as I can tell). To validate a JSON document against a schema, you first need to get all of the ids from the schema itself. The validation is then a simple case of iterating through the contents of the schema and checking all is present and correct in the JSON doc.


Just realised that if the JSON class can make a JSON doc from a var, then it must be extracting the identifiers. A quick look at the source (juce_JSON.cpp) in the lower section where the stuff related to constructing JSON files is, has answered my questions. If I make a neat validator, I’ll share. Thanks