5 ### How can I parse from a string?
8 json j = json::parse("[1,2,3,4]");
11 You can pass string literals (as above), `std::string`, `const char*` or byte containers such as `std::vector<uint8_t>`.
13 ### How can I parse from a file?
16 std::ifstream i("your_file.json");
17 json j = json::parse(i);
22 ### How can I serialize a JSON value
25 std::cout << j << std::endl;
31 std::string s = j.dump();
32 std::cout << s << std::endl;
35 ### How can I pretty-print a JSON value
38 std::cout << std::setw(4) << j << std::endl;
44 std::string s = j.dump(4);
45 std::cout << s << std::endl;
48 The number `4` denotes the number of spaces used for indentation.
52 ### How can I iterate over a JSON value?
57 // val is a reference for the current value
61 This works with any JSON value, also primitive values like numbers.
63 ### How can I access the keys when iterating over a JSON object?
66 for (auto it = j.begin(); it != j.end(); ++it)
69 json &val = it.value();
71 // the key (for objects)
72 const std::string &key = it.key();
76 You can also use an iteration wrapper and use range for:
79 for (auto it : json::iteration_wrapper(j))
82 json &val = it.value();
84 // the key (for objects)
85 const std::string &key = it.key();