boost创建JSON
时间:2010-06-28 来源:cdy_0
用boost的PropertyTree json_parser 生成json字符串
// test.cpp
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
using namespace std;
using namespace boost::property_tree;
int main(){
ptree pt_1,pt_11,pt_12;
pt_11.put("id","3445");
pt_11.put<int>("age",29);
pt_11.put("name","chen");
pt_12.push_back(make_pair("",pt_11));
pt_12.push_back(make_pair("",pt_11));
//replace or create child node "data"
pt_1.put_child("data",pt_12);
ostringstream os;
write_json(os,pt_1);
cout<<os.str()<<endl;
system("pause");
return 0;
}
// =========== 产生如下JSON串: ===============
/*
{
"data":
[
{
"id": "3445",
"age": "29",
"name": "chen"
},
{
"id": "3445",
"age": "29",
"name": "chen"
}
]
}
*/
// test.cpp
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
using namespace std;
using namespace boost::property_tree;
int main(){
ptree pt_1,pt_11,pt_12;
pt_11.put("id","3445");
pt_11.put<int>("age",29);
pt_11.put("name","chen");
pt_12.push_back(make_pair("",pt_11));
pt_12.push_back(make_pair("",pt_11));
//replace or create child node "data"
pt_1.put_child("data",pt_12);
ostringstream os;
write_json(os,pt_1);
cout<<os.str()<<endl;
system("pause");
return 0;
}
// =========== 产生如下JSON串: ===============
/*
{
"data":
[
{
"id": "3445",
"age": "29",
"name": "chen"
},
{
"id": "3445",
"age": "29",
"name": "chen"
}
]
}
*/
相关阅读 更多 +