【问题标题】:How to convert class object to json string using boost library in C++?如何使用 C++ 中的 boost 库将类对象转换为 json 字符串?
【发布时间】:2015-06-04 11:31:18
【问题描述】:

我对 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?

【问题讨论】:

  • “我是否必须先将这些对象放入容器(如矢量或列表)中,然后将其写入 JSON?” - 这与事实不符您使用 Boost Property Tree 有多个较早的问题。您是否使用 SO 作为代码编写服务?
  • @sehe :对不起 sehe 和其他所有人。下一次,在彻底解决问题后,我将非常具体且有限地提出问题,并且我将仅针对问题的特定部分而不是整个代码提出问题。我真的很抱歉。

标签: c++ json boost boost-propertytree


【解决方案1】:

这并不“相当容易”,因为 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"
    }
}

【讨论】:

    【解决方案2】:

    使用 boost 1.78.0,你可以使用this

    【讨论】:

      【解决方案3】:
      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”:{ “令牌号”:“1122”, “命令状态”:“0”, "CommandID": "00", “CPUID”:“A1234B1234”, “已加密”:“0” } }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-21
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多