我想要這樣的東西:
{ "rooms": [ "room1", "room2", "room3", etc ] }
我有一個std::vector<std::string>
房間的名稱,我想將其轉換為 JSON 的鍵將是“房間”,其值將是所有房間的串列。
總結一下,
如何將一個std::vector
JSON 陣列轉換為一個值(不是一個鍵)。
感謝幫助者!:)
uj5u.com熱心網友回復:
您可以直接從創建一個 Json 陣列,std::vector<std::string>
這樣就可以了:
#include <nlohmann/json.hpp>
#include <iostream>
#include <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"]}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/470834.html
標籤:C json nlohmann-json
上一篇:一個類的所有實體共享相同的值