#include <boost/property_tree/ptree.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace boost::property_tree;
boost::property_tree::ptree ptree_root;
ptree_root.put("root","me_root");
boost::property_tree::ptree ptree_items;
{
boost::property_tree::ptree pt; //一棵树
pt.put<int>("item_1",20);
pt.put<int>("item_2",40);
ptree_items.push_back(make_pair("",pt));
}
{
boost::property_tree::ptree pt;//又一棵树
pt.put("item_1","30");
pt.put("item_2","60");
ptree_items.push_back(make_pair("",pt));
}
ptree_root.put_child("items", ptree_items );
boost::property_tree::write_json("C:\\file_path.json",ptree_root);
try
{
/*std::stringstream ss;
ss << "{\"cmd\":\"19\",\"gwID\":\"00CD85462414\",\"devID\":\"695AA901004B1200\",\"type\":\"02\",\"data\":{\"status\":\"1\"}}";
boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt);
std::string cmd = pt.get<std::string>("cmd");
std::string gwID = pt.get<std::string>("gwID");
std::string devID = pt.get<std::string>("devID");
std::string type = pt.get<std::string>("type");*/
string s = "{\"age\" : 26,\"study\":{\"language\":{\"one\":\"chinese\",\"two\":\"math\"},\"fee\":500,\"subject\":[{\"one\":\"china\"},{\"one\":\"Eglish\"}]},\"person\":[{\"id\":1,\"name\":\"chen\"},{\"id\":2,\"name\":\"zhang\"}],\"name\" : \"huchao\"}";
ptree pt;
stringstream stream(s); //这步不知道为什么要这样
read_json<ptree>( stream, pt);
pt.put("study.language.one","physics");//修改数据(这步废了好久时间,最后通过读英文资料解决)
pt.put("study.fee",600);
string s1=pt.get<string>("age");
cout<<s1<<endl;
}
catch (std::exception const& e)
{
std::cerr << e.what() << std::endl;
}