1#include <iostream> 2#include <nlohmann/json.hpp> 3 4using json = nlohmann::json; 5using namespace nlohmann::literals; 6 7int main() 8{ 9 // create a JSON object with different entry types 10 json j = 11 { 12 {"integer", 1}, 13 {"floating", 42.23}, 14 {"string", "hello world"}, 15 {"boolean", true}, 16 {"object", {{"key1", 1}, {"key2", 2}}}, 17 {"array", {1, 2, 3}} 18 }; 19 20 // access existing values 21 int v_integer = j.value("/integer"_json_pointer, 0); 22 double v_floating = j.value("/floating"_json_pointer, 47.11); 23 24 // access nonexisting values and rely on default value 25 std::string v_string = j.value("/nonexisting"_json_pointer, "oops"); 26 bool v_boolean = j.value("/nonexisting"_json_pointer, false); 27 28 // output values 29 std::cout << std::boolalpha << v_integer << " " << v_floating 30 << " " << v_string << " " << v_boolean << "\n"; 31} 32