mirror of https://github.com/nlohmann/json.git
33 lines
889 B
C++
33 lines
889 B
C++
#include <iostream>
|
|
#include <string_view>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
using namespace std::string_view_literals;
|
|
using json = nlohmann::json;
|
|
|
|
int main()
|
|
{
|
|
// create a JSON object with different entry types
|
|
json j =
|
|
{
|
|
{"integer", 1},
|
|
{"floating", 42.23},
|
|
{"string", "hello world"},
|
|
{"boolean", true},
|
|
{"object", {{"key1", 1}, {"key2", 2}}},
|
|
{"array", {1, 2, 3}}
|
|
};
|
|
|
|
// access existing values
|
|
int v_integer = j.value("integer"sv, 0);
|
|
double v_floating = j.value("floating"sv, 47.11);
|
|
|
|
// access nonexisting values and rely on default value
|
|
std::string v_string = j.value("nonexisting"sv, "oops");
|
|
bool v_boolean = j.value("nonexisting"sv, false);
|
|
|
|
// output values
|
|
std::cout << std::boolalpha << v_integer << " " << v_floating
|
|
<< " " << v_string << " " << v_boolean << "\n";
|
|
}
|