json/docs/examples/operator_array__keytype.c++...

35 lines
734 B
C++
Raw Normal View History

#include <iostream>
2018-08-16 21:53:47 +02:00
#include <iomanip>
#include <string_view>
#include <nlohmann/json.hpp>
2015-06-25 00:40:16 +02:00
using namespace std::string_view_literals;
using json = nlohmann::json;
2015-06-25 00:40:16 +02:00
int main()
{
// create a JSON object
json object =
{
{"one", 1}, {"two", 2}, {"three", 2.9}
};
// output element with key "two"
std::cout << object["two"sv] << "\n\n";
2015-06-25 00:40:16 +02:00
// change element with key "three"
object["three"sv] = 3;
2015-06-25 00:40:16 +02:00
// output changed array
std::cout << std::setw(4) << object << "\n\n";
// mention nonexisting key
object["four"sv];
2015-06-25 00:40:16 +02:00
// write to nonexisting key
object["five"sv]["really"sv]["nested"sv] = true;
2015-06-25 00:40:16 +02:00
// output changed object
std::cout << std::setw(4) << object << '\n';
}