JSON11基本用法如下:
//JSON对象转json字符串
json11::Json my_json = json11::Json::object{
//json11支持的数据类型
{ "keystr","value1" },
{ "keybool",false },
{ "keyint",5 },
{ "keydouble",5.5},
{ "keyarr", json11::Json::array { 1, 2, 3,6,7 } },
{ "keyobj", json11::Json::object{{"key_obj","key_obj_val"}}},
};
std::string json_str = my_json.dump();
std::cout << "json_str: " << json_str << "\n";
json11::Json json = json11::Json::array{ json11::Json::object { { "key", "val" } } };
std::string str = json[0]["key"].string_value();
std::cout << "str===>>>: " << str << "\n";
//JSON字符串转对象
//const std::string simple_test = R"( {"k1":"v1", "k2":42, "k3":["a",123,true,false,null]} )";
const std::string simple_test = "{\"k1\":\"v1\", \"k2\":42, \"k3\":[\"a\",123,true,false,null]} ";
std::string err;
const auto json1 = json11::Json::parse(simple_test, err);
std::cout << "k1: " << json1["k1"].string_value() << "\n";
std::cout << "k3: " << json1["k3"].dump() << "\n";
for (auto &k : json1["k3"].array_items())
{
std::cout << " - " << k.dump() << "\n";
}