Parsing json with arrays

I couldn’t find any comprehensive examples of parsing json in juce especially when there was an array involved.
This is the json I need to parse.
{“total”:2,“notes”:[{“id”:6,“title”:“note 6”},{“id”:5,“title”:“note 5”}]}

I can get the total easy enough by doing this.

var jsonReply = JSON::parse(results);
String total= jsonReply.getProperty(“total”, var()).toString();

Not sure if that is even the right way to do that. But I have no idea how to get the array of objects out.
Any help would be appreciated.

It works exact the same way as you already figured out: a var can also represent an Array, so in this case you can use Array* var::getArray():

Array<var>* notesArray = jsonReply.getProperty ("notes").getArray();

Hope that helps…

OK, that didn’t work but this did.

Array* notesArray = jsonReply.getProperty(“inbox”, var()).getArray();

From there I want to get the notes. i tried this but it didn’t work.

for (int i = 0; i < notesArray->size(); ++i)
{
var note = notesArray[i];
String title = note.getProperty(“title”, var()).toString();
}

title is always an empty string.

You probably meant (*notesArray)[i]

And it’s much easier to use a C++!1 for loop, e.g.

if (auto notesArray = jsonReply.getProperty("inbox", var()).getArray())
    for (auto& note : *notesArray)
        title = note.getProperty etc...
1 Like

Thank you sir. That did the trick.