代码:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
using namespace boost::property_tree;
int main(int argc, char* argv[])
{
ptree tree;
//1. read_json<ptree>()、istringstream
istringstream iss("{\"KXFR_ID\":123,\"ZONE\":\"ffdns.net\"}");
read_json<ptree>(iss, tree);
string kxfr_id = tree.get_child("KXFR_ID").get_value<string>("KXFR_ID");
string zone = tree.get_child("ZONE").get_value<string>("ZONE");
cout<<"1. read_json<ptree>:"<<endl<<"kxfr_id:"<<lexical_cast<int>(kxfr_id)<<", zone:"<<zone<<endl<<endl;
//2. write_json<ptree>()、ostringstream
ostringstream oss;
write_json<ptree>(oss, tree);
cout<<"2. write_json<ptree>:"<<endl<<oss.str()<<endl;
return 0;
}
输出结果:
参考资料:
Boost.PropertyTree:http://www.boost.org/doc/libs/1_49_0/doc/html/property_tree.html