书写格式
①最外层用 { }扩起来
②里面的键 值对 用 { }扩起来
③键对应的值超过一个 值也要用{ }扩起来
json json_config = {{"action", "run"},
{"func", "print"},
{"layer", ""},
{"parameters",
{{"baseline_path", ""},
{"config_path", ""},
{"curve_compare", ""},
{"op_id", -1},
{"op_name", ""},
{"output_dir", "/tmp/idc/op_dump"},
{"result_path", ""},
{"source_path", ""}}}};
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
https://github.com/nlohmann/json#cmake
C++对json文件的读写
https://blog.csdn.net/m0_47584501/article/details/118461020
花括号{}代表的是一个map
中括号[]代表的是一个vector
{
"DataSetSpec":{
"upBound": 0.01,
"lowBound": 0.95
},
"K":50
}
//读取后得到json对象 取值 auto upbound_ = json[DataSetSpec][upBound]
{
"DataSetSpec":[{
"upBound": 0.01,
"lowBound": 0.95
},
"K":50
]
}
//读取后得到json对象 取值 auto upbound_ = json[DataSetSpec][0][upBound]
[
{
"upBound": 0.01,
"lowBound": 0.95
},
{
"K": 50
}
]
//读取后得到json对象 取值 auto upbound_ = json[0][upBound]