使用 nlohman json 为 vector<bool> 创建 json 对象

create json object for vector<bool> using nlohman json

我想使用 nlohmann json libvector<bool> vec = {true, false,true} 对象转换为 json 对象通过 rest api 发送。

我希望转换后的 json 对象的形式为

{
  "data" : [true, false, true]
}

要在 nlohmann/json 中执行此操作,所有需要做的就是创建一个空的 nlohmann JSON 对象并将布尔向量分配给字段“数据”。

例如

std::vector<bool> vec = {true, false, true};

nlohmann::json j;
j["data"] = vec;

产生 json 对象

{
    "data":[true,false,true]
}