summaryrefslogtreecommitdiffstats
path: root/doc_example/json_doc_1.cpp
blob: cb5449f39caf7b24154d20ec2288bb615d79679f (plain)
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
#include <orcus/json_document_tree.hpp>
#include <orcus/config.hpp>

#include <cstdlib>
#include <iostream>

using namespace std;

const char* json_string = "{"
"   \"name\": \"John Doe\","
"   \"occupation\": \"Software Engineer\","
"   \"score\": [89, 67, 90]"
"}";

int main()
{
    using node = orcus::json::node;

    orcus::json_config config; // Use default configuration.

    orcus::json::document_tree doc;
    doc.load(json_string, config);

    // Root is an object containing three key-value pairs.
    node root = doc.get_document_root();

    for (std::string_view key : root.keys())
    {
        node value = root.child(key);
        switch (value.type())
        {
            case orcus::json::node_t::string:
                // string value
                cout << key << ": " << value.string_value() << endl;
                break;
            case orcus::json::node_t::array:
            {
                // array value
                cout << key << ":" << endl;

                for (size_t i = 0; i < value.child_count(); ++i)
                {
                    node array_element = value.child(i);
                    cout << "  - " << array_element.numeric_value() << endl;
                }
                break;
            }
            default:
                ;
        }
    }

    return EXIT_SUCCESS;
}