我现在很享受这个。 1. 你发布一个非问题 2. 我发布一个匹配的答案。
如果您下次想自己做这个技巧,请看这里:http://paste.ubuntu.com/11665634/(基于How return leaf nodes of a boost::property_tree)。
现在是派对技巧:
Live On Coliru
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
#include <sstream>
using boost::property_tree::ptree;
int main()
{
ptree pt;
pt.add("TokenNo", "Deb123456");
pt.add("CPUID", "Deb123456");
pt.add("CommandID", "05");
pt.add("CommandStatus", "null");
pt.add("IsEncrypted", "0");
ptree cs;
cs.add("Add", "97");
cs.add("EstbCD", "99999999");
cs.add("EID", "XY");
cs.add("CID", "0154400000");
cs.add("DATE", "14042015");
cs.add("TIME", "1835");
cs.add("IOMODE", "I");
cs.add("REASONCODE", "55");
cs.add("LAT", "87");
#if 1 // if you are a sane person
pt.put_child("CommandString", cs);
#else
// or, if you really wanted braindead non-JSON:
std::ostringstream oss;
write_json(oss, cs);
pt.put("CommandString", oss.str());
#endif
write_json(std::cout, pt);
}
sane people 的输出:
{
"TokenNo": "Deb123456",
"CPUID": "Deb123456",
"CommandID": "05",
"CommandStatus": "null",
"IsEncrypted": "0",
"CommandString": {
"Add": "97",
"EstbCD": "99999999",
"EID": "XY",
"CID": "0154400000",
"DATE": "14042015",
"TIME": "1835",
"IOMODE": "I",
"REASONCODE": "55",
"LAT": "87"
}
}
braindead people requirements 的输出:
{
"TokenNo": "Deb123456",
"CPUID": "Deb123456",
"CommandID": "05",
"CommandStatus": "null",
"IsEncrypted": "0",
"CommandString": "{\n \"Add\": \"97\",\n \"EstbCD\": \"99999999\",\n \"EID\": \"XY\",\n \"CID\": \"0154400000\",\n \"DATE\": \"14042015\",\n \"TIME\": \"1835\",\n \"IOMODE\": \"I\",\n \"REASONCODE\": \"55\",\n \"LAT\": \"87\"\n}\n"
}