forked from gregjesl/simpleson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrootarray.cpp
56 lines (48 loc) · 1.41 KB
/
rootarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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
json::jobject example = json::jobject::parse(test);
// 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"));
}