c++ - 如何将向量转换为 Json 对象?C++
问题描述
我有一个 RoomData 对象的向量,这里是对象:
typedef struct RoomData
{
unsigned int id;
std::string name;
std::string maxPlayers;
unsigned int questionCount;
unsigned int timePerQuestion;
unsigned int isActive;
} RoomData;
我需要将此向量转换为 json 对象。我怎样才能做到这一点?我不知道
解决方案
jsoncons和nlohmann都支持 JSON 和 C++ 对象之间的转换,Martin York 的ThorsSerializer 也是如此(见他的回答。)
给定一个 RoomData 向量,即:
namespace ns {
struct RoomData
{
unsigned int id;
std::string name;
std::string maxPlayers;
unsigned int questionCount;
unsigned int timePerQuestion;
unsigned int isActive;
} RoomData;
}
std::vector<ns::RoomData> rooms;
rooms.push_back(ns::RoomData{ 1, "Room 1", "Few", 2, 56, 1 });
rooms.push_back(ns::RoomData{ 2, "Room 2", "Lots", 2, 56, 1 });
使用jsoncons转换为 JSON 并返回:
#include <iostream>
#include <jsoncons/json.hpp>
namespace jc = jsoncons;
// Declare the traits. Specify which data members need to be serialized.
JSONCONS_ALL_MEMBER_TRAITS(ns::RoomData, id, name, maxPlayers,
questionCount, timePerQuestion, isActive);
int main()
{
std::vector<ns::RoomData> rooms;
rooms.push_back(ns::RoomData{ 1, "Room 1", "Few", 2, 56, 1 });
rooms.push_back(ns::RoomData{ 2, "Room 2", "Lots", 2, 56, 1 });
std::string s;
jc::encode_json(rooms, s, jc::indenting::indent);
std::cout << "(1)\n" << s << "\n\n";
auto rooms2 = jc::decode_json<std::vector<ns::RoomData>>(s);
std::cout << "(2)\n";
for (auto item : rooms2)
{
std::cout << "id: " << item.id << ", name: " << item.name
<< ", maxPlayers: " << item.maxPlayers << "\n";
}
}
输出:
(1)
[
{
"id": 1,
"isActive": 1,
"maxPlayers": "Few",
"name": "Room 1",
"questionCount": 2,
"timePerQuestion": 56
},
{
"id": 2,
"isActive": 1,
"maxPlayers": "Lots",
"name": "Room 2",
"questionCount": 2,
"timePerQuestion": 56
}
]
(2)
id: 1, name: Room 1, maxPlayers: Few
id: 2, name: Room 2, maxPlayers: Lots
使用nlohmann转换为 JSON 并返回:
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
namespace nh = nlohmann;
// Provide from_json and to_json functions in the same namespace as your type
namespace ns {
void from_json(const nh::json& j, ns::RoomData& val)
{
j.at("id").get_to(val.id);
j.at("name").get_to(val.name);
j.at("maxPlayers").get_to(val.maxPlayers);
j.at("questionCount").get_to(val.questionCount);
j.at("timePerQuestion").get_to(val.timePerQuestion);
j.at("isActive").get_to(val.isActive);
}
void to_json(nh::json& j, const ns::RoomData& val)
{
j["id"] = val.id;
j["name"] = val.name;
j["maxPlayers"] = val.maxPlayers;
j["questionCount"] = val.questionCount;
j["timePerQuestion"] = val.timePerQuestion;
j["isActive"] = val.isActive;
}
} // namespace ns
int main()
{
std::vector<ns::RoomData> rooms;
rooms.push_back(ns::RoomData{ 1, "Room 1", "Few", 2, 56, 1 });
rooms.push_back(ns::RoomData{ 2, "Room 2", "Lots", 2, 56, 1 });
std::stringstream ss;
nh::json j = rooms;
ss << std::setw(4) << j;
std::cout << "(1)\n" << ss.str() << "\n\n";
nh::json j2 = nh::json::parse(ss.str());
auto rooms2 = j2.get<std::vector<ns::RoomData>>();
std::cout << "(2)\n";
for (auto item : rooms2)
{
std::cout << "id: " << item.id << ", name: " << item.name
<< ", maxPlayers: " << item.maxPlayers << "\n";
}
}
输出:
(1)
[
{
"id": 1,
"isActive": 1,
"maxPlayers": "Few",
"name": "Room 1",
"questionCount": 2,
"timePerQuestion": 56
},
{
"id": 2,
"isActive": 1,
"maxPlayers": "Lots",
"name": "Room 2",
"questionCount": 2,
"timePerQuestion": 56
}
]
(2)
id: 1, name: Room 1, maxPlayers: Few
id: 2, name: Room 2, maxPlayers: Lots
推荐阅读
- css - CSS 旋转动画在 iOS 和 Mac 上的 Safari 上不旋转
- c# - 无法加载 DLL 'tensorflow' ML.Net & .Net Framework 4.6.2
- google-cloud-platform - Google Cloud Platform:有什么方法可以重新排列现有数据目录模板中的字段?
- python - mysql.connector.errors.NotSupportedError:不支持身份验证插件“caching_sha2_password”仅在 exe 中显示
- apache-kafka - 如何通过 docker-compose 实现 debezium 连接器(Oracle)?
- go - 如果项目存在于使用 Golang 和 bson 的数组中,则过滤 mongo 集合
- django - 在 Django 中重新创建应用迁移
- node.js - macOS 11.1 Big Sur 上的 Puppeteer 和 Headless Chrome Crawler
- python - 如何使用 openvino 的 opencv 从 UDP 捕获视频
- java - 正则表达式验证无法停止无效数据