nlohmann json 将键值从字符串修改为数组
如下,原数据是这样
j["example"]="hello example";
修改成
j["example"]=json({"example2", "hello example2"});

好像直接这样就可以了.

测试验证

json j;
j["example"] = "hello example";
cout << j.dump(4) << endl;
j["example"] = json({"example2", "hello example2"});
cout << j.dump(4) << endl;

结果

{
    "example": "hello example"
}
{
    "example": [
        "example2",
        "hello example2"
    ]
}

测试代码

json j;
j["example"] = "hello example";
cout << j.dump(4) << endl;
j["example"] = json::array();
json j1, j2;
j1["one"] = 1;
j2["two"] = 2;
j["example"].push_back(j1);
j["example"].push_back(j2);
cout << j.dump(4) << endl;

运行结果:

{
    "example": "hello example"
}
{
    "example": [
        {
            "one": 1
        },
        {
            "two": 2
        }
    ]
}
本文链接地址:https://const.net.cn/720.html

标签: json

添加新评论