您可以直接从
std::vector<std::string>
所以类似的方法会奏效:
#include <nlohmann/json.hpp>
#include <iostream>
#include <std::string>
#include <vector>
using json = nlohmann::json;
int main() {
std::vector<std::string> rooms{
"room1",
"room2",
"room3",
};
json j;
// key `rooms` and create the json array from the vector:
j["rooms"] = rooms;
std::cout << j << '\n';
}
输出
{"rooms":["room1","room2","room3"]}