首页 > 解决方案 > 如何将向量转换为 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 对象。我怎样才能做到这一点?我不知道

标签: c++json

解决方案


jsonconsnlohmann都支持 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

推荐阅读