2017-07-09 11:51:38 +02:00
|
|
|
#include <iostream>
|
2018-02-01 22:20:26 +01:00
|
|
|
#include <nlohmann/json.hpp>
|
2015-06-24 12:15:51 +02:00
|
|
|
|
2016-01-30 20:23:14 +01:00
|
|
|
using json = nlohmann::json;
|
2015-06-24 12:15:51 +02:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// create JSON array
|
|
|
|
json array = {"first", "2nd", "third", "fourth"};
|
|
|
|
|
|
|
|
// output element at index 2 (third element)
|
|
|
|
std::cout << array.at(2) << '\n';
|
|
|
|
|
|
|
|
// change element at index 1 (second element) to "second"
|
|
|
|
array.at(1) = "second";
|
|
|
|
|
|
|
|
// output changed array
|
|
|
|
std::cout << array << '\n';
|
|
|
|
|
2017-03-12 13:49:39 +01:00
|
|
|
// exception type_error.304
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// use at() on a non-array type
|
|
|
|
json str = "I am a string";
|
|
|
|
str.at(0) = "Another string";
|
|
|
|
}
|
|
|
|
catch (json::type_error& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
// exception out_of_range.401
|
2015-06-24 12:15:51 +02:00
|
|
|
try
|
|
|
|
{
|
2017-03-12 13:49:39 +01:00
|
|
|
// try to write beyond the array limit
|
2015-06-24 12:15:51 +02:00
|
|
|
array.at(5) = "sixth";
|
|
|
|
}
|
2017-03-08 21:03:19 +01:00
|
|
|
catch (json::out_of_range& e)
|
2015-06-24 12:15:51 +02:00
|
|
|
{
|
2017-03-08 21:03:19 +01:00
|
|
|
std::cout << e.what() << '\n';
|
2015-06-24 12:15:51 +02:00
|
|
|
}
|
|
|
|
}
|