C++ json格式的书写

书写格式
①最外层用 { }扩起来
②里面的键 值对 用 { }扩起来
③键对应的值超过一个 值也要用{ }扩起来

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]

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值