#include <QtCore/QCoreApplication> #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 <iostream> using namespace std; using namespace boost::property_tree; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); 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/"}";
string s = "{age : 26,study:{
language:{one:chinese,two:math},
fee:,
subject:[{one:china},{one:Eglish}]
},
person:[{id:,name:chen},{id:,name:zhang}],
name : huchao}";
ptree pt; stringstream stream(s); //这步不知道为什么要这样 read_json<ptree>( stream, pt); pt.put("study.language.one","physics");//修改数据(这步废了好久时间,最后通过读英文资料解决) pt.put("study.fee",); string s1=pt.get<string>("age"); cout<<s1<<endl; string s2=pt.get<string>("name"); cout<<s2<<endl; string s3=pt.get_child("study").get_child("language").get<string>("one"); cout<<s3<<endl; string s4=pt.get_child("study").get<string>("fee"); cout<<s4<<endl; ptree p1,p2; p1 = pt.get_child("study").get_child("subject");//访问多级节点中的数组数据 for (ptree::iterator it = p1.begin(); it != p1.end(); ++it) { p2 = it->second; //first为空 cout<<"subject="<<p2.get<string>("one")<<endl; } return ; return a.exec(); } // pt.put("conf.theme", "Matrix Reloaded"); // pt.put("conf.clock_style", 13); // pt.put("conf.gui", 0); // pt.put("conf.urls.url","http://www.url4.org"); // pt.add("conf.urls.url","http://www.url4.org"); // write_json("conf.json", pt); // read_json("conf.json",pt); // cout<< pt.get<string>("conf.theme") <<endl; // cout<< pt.get<int>("conf.clock_style") <<endl; // cout<< pt.get<long>("conf.gui") <<endl; // cout<< pt.get("conf.no_prop", 100) <<endl; // BOOST_AUTO(child, pt.get_child("conf.urls")); // for(BOOST_AUTO(pos,child.begin()); pos != child.end(); ++pos) // { // cout<<pos->second.data()<<","; // } // cout<<endl; // 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()<
里面的有些还是比较模糊,原理不清
vs提示个错误:pnode.get<int>("x")不存在从 "char [2]" 转换到 "boost::property_tree::string_path<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,boost::property_tree::id_translator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >" 的适当构造函数
查了半天结果可以运行,这VS也有点坑人吧