json/doc/examples/README.cpp

37 lines
689 B
C++
Raw Normal View History

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