我有这个字段的RoomData
类:
#include <string>
class RoomData
{
public:
int id;
string name;
int maxPlayers;
int timePerQuestion;
int isActive;
};
并且我试图将RoomData Vector<RoomData>
的vactor转换为json数组
这就是我的尝试:
#include <nlohmann/json.hpp>
using nlohmann::json;
string serialize(vector<RoomData> roomData)
{
json j(roomData);
string jsonArray = j.dump();
return jsonArray;
}
但它给了我这些错误:
C2338 forcing MSVC stacktrace to show which T we're talking about.
C2338 could not find to_json() method in T's namespace
C2065 'force_msvc_stacktrace': undeclared identifier
C2825 'decayed': must be a class or namespace when followed by '::'
C2510 'decayed': left of '::' must be a class/struct/union
答案 0 :(得分:1)
我猜你正在使用https://github.com/nlohmann/json。
要使其适用于您的某种类型,您只需提供两个功能:
using nlohmann::json;
void to_json(json& j, const RoomData& r) {
j = json{
{"id", r.id},
{"name", r.name},
{"maxPlayers", r.maxPlayers},
{"timePerQuestion", r.timePerQuestion},
{"isActive", r.isActive}
};
}
void from_json(const json& j, RoomData& r) {
r.id = j.at("id").get<int>();
r.name = j.at("name").get<std::string>();
r.maxPlayers = j.at("maxPlayers").get<int>();
r.timePerQuestion = j.at("timePerQuestion").get<int>();
r.isActive = j.at("timePerQuestion").get<int>();
}
然后这将有效。希望这会有所帮助。欢呼声。