我尝试将“人物”对象的向量存储为nlohmann :: JSON对象。
我该怎么办?
这是Person结构:
struct RoomData
{
unsigned int id;
std::string name;
unsigned int isActive;
};
我曾尝试编写to_json函数,希望它能帮上忙-
namespace ns
{
void to_json(nlohmann::json& j, const Person& p)
{
j = nlohmann::json
{
{ "id", p.id },
{ "name", p.name },
{ "isActive", p.isActive }
};
}
void from_json(const nlohmann::json& j, Person& p) {
j.at("id").get_to(p.id);
j.at("name").get_to(p.name);
j.at("isActive").get_to(p.isActive);
}
}
然后我开始编写一个函数,该函数会将我的Person向量转换为 json,然后向后(从json到人向量)
void to_vector_of_person_json(nlohmann::json& j,std::vector<Person> v)
{
for (auto const& value : v)
{
nlohmann::json j2;
to_json(j2, value);
j. // I don't know what to do here... please help.
}
}
void from_json_to_person_vector(nlohmann::json j, std::vector<Person> &v)
{
//..... I don't know .. please help.
}
能帮我实现这些功能吗?还是找到一种更好的方法来实现这些目标?谢谢。