如何将矢量转换为Json对象? C ++

时间:2018-06-16 01:11:28

标签: c++ json

我有一个RoomData对象的向量,这是对象:

typedef struct RoomData
{
unsigned int id;
std::string name;
std::string maxPlayers;
unsigned int questionCount;
unsigned int timePerQuestion;
unsigned int isActive;
} RoomData;

我需要将此向量转换为json对象。 我怎样才能做到这一点?我不知道

3 个答案:

答案 0 :(得分:0)

您应该使用json库。 nlohmann/json现在是不错的选择,但您可以按性能和效果here找到比较。

您也可以省略C ++中的var newRow = document.createElement("tr"); var oldRow = document.getElementById("searchOutput"); newRow.innerHTML = "<tr><td>Middle</td></tr>"; document.getElementByTagName("table").insertBefore(row, oldRow); ,它会自动创建一个。

答案 1 :(得分:0)

我个人喜欢ThorsSerializer

序列化数据就变得微不足道了

#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include <iostream>
#include <string>

struct RoomData
{
    unsigned int id;
    std::string name;
    std::string maxPlayers;
    unsigned int questionCount;
    unsigned int timePerQuestion;
    unsigned int isActive;
};

ThorsAnvil_MakeTrait(RoomData, id, name, maxPlayers, questionCount, timePerQuestion, isActive);

示例用法:

int main()
{
    using ThorsAnvil::Serialize::jsonImport;
    using ThorsAnvil::Serialize::jsonExport;

    RoomData    data{6, "Bed Room", "Lots of them", 2, 56, 1};

    std::cout << jsonExport(data) << "\n";
}

输出:

{
    "id": 6,
    "name": "Bed Room",
    "maxPlayers": "Lots of them",
    "questionCount": 2,
    "timePerQuestion": 56,
    "isActive": 1
}

答案 2 :(得分:0)

jsonconsnlohmann都支持JSON和C ++对象之间的转换,马丁·约克(Martin York)的ThorsSerializer(请参见他的回答)。

给出RoomData的向量,即:

namespace ns {
    typedef 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_TYPE_TRAITS_DECL(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