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

时间:2015-03-31 07:50:25

标签: c++ json boost boost-propertytree

我对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文件中声明的另一个类。

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

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

2 个答案:

答案 0 :(得分:6)

这不是“相当容易”,因为C ++没有JSON支持。

Boost也没有:

那就是说,这似乎是你想要的:

  

所以,我想将类对象转换为JSON字符串,我该怎么做?我是否必须先将这些对象放入容器(如矢量或列表)中,然后将其写入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"
    }
}

答案 1 :(得分:-1)

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);

OUT PUT

{     &#34; POST1&#34;:{         &#34; TokenNo&#34;:&#34; 1122&#34;,         &#34; CommandStatus&#34;:&#34; 0&#34;,         &#34; CommandID&#34;:&#34; 00&#34;,         &#34; CPUID&#34;:&#34; A1234B1234&#34;,         &#34; ISEncrypted&#34;:&#34; 0&#34;     } }