boost生成json

#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>using namespace std;
using namespace boost::property_tree; 

int main()
{
    ptree pt;
    ptree children;
    ptree child1, child2, child3;

    child1.put("", 1);
    child2.put("", 2);
    child3.put("", 3);
    //write_json("test2.json", child1); //put空字符串后,马上转json会报错。

    children.push_back(std::make_pair("", child1));
    children.push_back(std::make_pair("", child2));
    children.push_back(std::make_pair("", child3));
    write_json("test3.json", children); //此时就不会报错

    pt.add_child("MyArray", children);

    write_json("test1.json", pt);
    return 0;
}
test1.json内容:
{
"MyArray": [
"1",
"2",
"3"
]
}
test3.json内容:

{
"": "1",
"": "2",
"": "3"
}

猜你喜欢

转载自www.cnblogs.com/Stephen-Qin/p/10157081.html