如何使用C+;中的boost库将类对象转换为json字符串+;? 我对C++很陌生,如果你觉得这很容易,我会先道歉。
我有以下文件 POST1.h如何使用C+;中的boost库将类对象转换为json字符串+;? 我对C++很陌生,如果你觉得这很容易,我会先道歉。,c++,json,boost,boost-propertytree,C++,Json,Boost,Boost Propertytree,我有以下文件 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::pr
#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
\ifndef POST1\u HH
#定义POST1_HH
#包括
#包括
#包括
使用名称空间std;
使用boost::property_tree::ptree;
使用boost::property_tree::read_json;
使用boost::property_tree::write_json;
使用boost::property_tree::basic_ptree;
#包括“DBAccess2.h”
一级邮政
{
公众:
字符串标记号;
字符串命令状态;
字符串CommandID;
字符串CPUID;
字符串被加密;
字符串JSON\u Cmnd\u字符串;
无效后设备状态(sqliteDB和DB\U OBJ);
};
#恩迪夫
下面是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);
}
#包括
#包括
#包括
#包括
#包括“DBAccess2.h”
#包括“POST1.h”
使用名称空间std;
使用boost::property_tree::ptree;
使用boost::property_tree::read_json;
使用boost::property_tree::write_json;
使用boost::property_tree::basic_ptree;
void POST1::POST_设备_状态(sqliteDB&DB_OBJ)
{
邮政1邮政局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_字符串中
所以,我想把class对象转换成JSON字符串,我该怎么做呢?
我是否必须首先将这些对象放入容器(如vector或list)中,然后将其写入JSON > P>这不是“相当容易”,因为C++没有JSON支持。
这两者都不能促进:
boost::property\u tree::ptree
:
#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”:{
“令牌号”:“1122”,
“CommandStatus”:“0”,
“CommandID”:“00”,
“CPUID”:“A1234B1234”,
“已加密”:“0”
}
}“我必须先将这些对象放入容器(如vector或list)中,然后将其写入JSON吗?”-这与之前使用Boost属性树提出的多个问题不符。你用SO来写代码吗?@sehe:对不起sehe和其他人。下一次,我将非常具体和有限的问题后,问他们彻底的工作,我会根据我的问题的特定部分,而不是整个代码。我真的很抱歉。
{
"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);