【发布时间】:2017-09-08 18:02:28
【问题描述】:
我想使用下面的代码 sn-p 打印 json 数组,但它没有给出想要的结果。这是输出
{
"prefix": "standard",
"faceID": "42"
}
{
"prefix1": "standard2",
"faceID2": "44"
}
这是由以下代码 sn-p 产生的:
#include <boost/serialization/string.hpp>
#include <string>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree;
using boost::property_tree::write_json;
void create_array(ptree parent)
{
std::stringstream oss;
write_json(oss,parent);
std::string serialized_strings(oss.str());
std::cout << oss.str() << std::endl;
}
int main()
{
ptree pt,pt1;
pt.put("prefix","standard");
pt.put("faceID",42);
create_array(pt);
pt1.put("prefix1","standard2");
pt1.put("faceID2",44);
create_array(pt1);
}
预期输出:
[
{
"prefix": "standard",
"faceID": "42"
},
{
"prefix1": "standard2",
"faceID2": "44"
}
]
【问题讨论】:
-
拜托,即使阅读大约 20 行的文档太多了,搜索也不会那么难。
-
亲爱的@sehe:我已经浏览了那个链接,看起来在 boost 中没有解决这个问题的方法,我不想创建数组的名称来绑定元素所以寻找替代解决方案。
-
你现在不“想要”什么? “创建名称”?你读过the limitations in those lines of documentation吗? (链接固定)
-
以下链接中给出的解决方案不符合我的要求。stackoverflow.com/questions/2114466/…