如何在C ++中使用Boost库将类对象转换为json字符串? [英] How to convert class object to json string using boost library in C++?

查看:384
本文介绍了如何在C ++中使用Boost库将类对象转换为json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++还是很陌生,如果您觉得这很简单,我会道歉.

I am fairly new to C++ and I apologise beforehand if you find this very easy.

我有以下文件 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文件中声明的另一个类.

(1) sqliteDB is another class declared in a .cpp file.

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

(2) the output of function dump() is a json string. this get stored into JSON_Cmnd_string.

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

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?

推荐答案

这不是相当容易",因为C ++不支持JSON.

This is not "fairly easy", because C++ doesn't have JSON support.

Boost也没有:

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

That said, this appears to be what you want:

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

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:

Yes, you put them into a tree container, namely boost::property_tree::ptree:

在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"
    }
}

这篇关于如何在C ++中使用Boost库将类对象转换为json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆