diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:48:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:48:59 +0000 |
commit | c484829272cd13a738e35412498e12f2c9a194ac (patch) | |
tree | a1f5ec09629ee895bd3963fa8820b45f2f4c574b /doc_example/json_doc_1.cpp | |
parent | Initial commit. (diff) | |
download | liborcus-upstream.tar.xz liborcus-upstream.zip |
Adding upstream version 0.19.2.upstream/0.19.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'doc_example/json_doc_1.cpp')
-rw-r--r-- | doc_example/json_doc_1.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/doc_example/json_doc_1.cpp b/doc_example/json_doc_1.cpp new file mode 100644 index 0000000..cb5449f --- /dev/null +++ b/doc_example/json_doc_1.cpp @@ -0,0 +1,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; +} |