Simpleson
rootarray.cpp

This is an example of how to handle JSON where the root object is an array

#include "json.h"
#include <stdio.h>
#include <assert.h>
int main(void)
{
// Create a couple objects
const std::string test =
"["
" {"
" \"firstName\": \"Jimmy\","
" \"lastName\": \"D\","
" \"hobbies\": ["
" {"
" \"sport\": \"tennis\""
" },"
" {"
" \"music\": \"rock\""
" }"
" ]"
" },"
" {"
" \"firstName\": \"Sussi\","
" \"lastName\": \"Q\","
" \"hobbies\": ["
" {"
" \"sport\": \"volleyball\""
" },"
" {"
" \"music\": \"classical\""
" }"
" ]"
" }"
"]";
// Parse the test array
// Access the data
std::string music_desired = example.array(0).get("hobbies").array(1).get("music").as_string();
// Print the data
printf("Music desired: %s\n", music_desired.c_str()); // Returns "rock"
// Check the result
assert(music_desired == std::string("rock"));
// Access the second entry
music_desired = example.array(1).get("hobbies").array(1).get("music").as_string();
// Print the data
printf("Music desired: %s\n", music_desired.c_str()); // Returns "classical"
// Check the result
assert(music_desired == std::string("classical"));
}
const_value array(const size_t index) const
Returns another constant value from this array.
Definition: json.h:826
const_value get(const std::string &key) const
Returns another constant value from this object.
Definition: json.h:814
std::string as_string() const
Returns a string representation of the value.
Definition: json.h:636
The class used for manipulating JSON objects and arrays.
Definition: json.h:410
static jobject parse(const char *input)
Parses a serialized JSON string.
Definition: json.cpp:887
const jobject::const_value array(const size_t index) const
Returns the value of an element in an array.
Definition: json.h:1066
Simpleson header file.