json/doc/examples/README.cpp

40 lines
753 B
C++
Raw Normal View History

#include <iostream>
2018-08-16 22:53:47 +03:00
#include <iomanip>
#include <nlohmann/json.hpp>
2015-06-30 00:02:41 +03:00
using json = nlohmann::json;
2015-06-30 00:02:41 +03:00
int main()
{
// create a JSON object
2015-06-30 01:12:18 +03:00
json j =
{
2015-06-30 00:02:41 +03:00
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
2015-06-30 01:12:18 +03:00
{
"answer", {
{"everything", 42}
}
},
2015-06-30 00:02:41 +03:00
{"list", {1, 0, 2}},
2015-06-30 01:12:18 +03:00
{
"object", {
{"currency", "USD"},
{"value", 42.99}
}
}
2015-06-30 00:02:41 +03:00
};
2015-06-30 01:12:18 +03:00
2015-06-30 00:02:41 +03:00
// add new values
j["new"]["key"]["value"] = {"another", "list"};
// count elements
auto s = j.size();
j["size"] = s;
2015-06-30 01:12:18 +03:00
2015-06-30 00:02:41 +03:00
// pretty print with indent of 4 spaces
std::cout << std::setw(4) << j << '\n';
}