如何在 C++ 中使用 boost 库将 class 对象转换为 json 字符串?

How to convert class object to json string using boost library in C++?

我是 C++ 的新手,如果您觉得这很容易,我先向您道歉。

我有以下文件 POST1.h

#ifndef POST1_HH
#define POST1_HH

#include <iostream>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using namespace std ;
using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;
using boost::property_tree::basic_ptree;

#include "DBAccess2.h"

class POST1
{

    public:
    string TokenNo;
    string CommandStatus;
    string CommandID;
    string CPUID;
    string ISEncrypted;
    string JSON_Cmnd_String;

    void POST_Device_Status(sqliteDB & DB_OBJ);

};

#endif

下面是POST1.cpp

#include <iostream>
#include <sstream>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include "DBAccess2.h"
#include "POST1.h"

using namespace std ;
using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;
using boost::property_tree::basic_ptree;

void POST1::POST_Device_Status(sqliteDB & DB_OBJ)
{
    POST1 POST_OBJ;

    POST_OBJ.TokenNo = "1122";
    POST_OBJ.CommandStatus = "0";
    POST_OBJ.CommandID = "00";
    POST_OBJ.CPUID = "A1234B1234";
    POST_OBJ.ISEncrypted = "0";
    POST_OBJ.JSON_Cmnd_String = DB_OBJ.dump(DB_OBJ);
}

注意:-

(1) sqliteDB 是在 .cpp 文件中声明的另一个 class。

(2) 函数 dump() 的输出是一个 json 字符串。这被存储到 JSON_Cmnd_string.

所以,我想将 class 对象转换为 JSON 字符串,我该怎么做? 我是否必须先将这些对象放入容器(如向量或列表)中,然后将其写入 JSON?

这不是 "fairly easy",因为 C++ 没有 JSON 支持。

Boost 也没有:

也就是说,这似乎是您想要的:

So, I want to convert the class object into JSON string, How can I do that ? Do I have to first put these object into a container (like vector or list) and then write it into JSON?

是的,你把它们放到一个树容器里,即boost::property_tree::ptree:

Live On Coliru

#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <iostream>
#include <sstream>
using boost::property_tree::ptree;

namespace Entities {

    struct POST1 {
        std::string TokenNo;
        std::string CommandStatus;
        std::string CommandID;
        std::string CPUID;
        std::string ISEncrypted;

    };

    std::string to_json(POST1 const& o) {
        ptree out;
        out.put("POST1.TokenNo",          o.TokenNo);
        out.put("POST1.CommandStatus",    o.CommandStatus);
        out.put("POST1.CommandID",        o.CommandID);
        out.put("POST1.CPUID",            o.CPUID);
        out.put("POST1.ISEncrypted",      o.ISEncrypted);

        std::ostringstream oss;
        boost::property_tree::write_json(oss, out);
        return oss.str();
    }
}

// ADL trigger; `using Entities::to_json` would be roughly equivalent, but not
// make it clear that ADL is happening
void to_json();

int main() {
    Entities::POST1 obj { "1122", "0", "00", "A1234B1234", "0" };
    std::cout << to_json(obj);
}

输出:

{
    "POST1": {
        "TokenNo": "1122",
        "CommandStatus": "0",
        "CommandID": "00",
        "CPUID": "A1234B1234",
        "ISEncrypted": "0"
    }
}
use this simple way

pt::ptree root;


    root.put("POST1 .TokenNo", "1122");
    root.put("POST1 .CommandStatus", "0");
    root.put("POST1 .CommandID", "00");
    root.put("POST1 .CPUID", "A1234B1234");
    root.put("POST1 .ISEncrypted", "0");
    // Once our ptree was constructed, we can generate JSON on standard output

    pt::write_json(std::cout, root);

输出

{ "POST1":{ "TokenNo": "1122", "CommandStatus": "0", "CommandID": "00", "CPUID": "A1234B1234", "ISEncrypted":“0” } }

使用 boost 1.78.0,您可以使用 this