#include <iostream> #include <string> #include <vector> #include <sstream> #include <boost/foreach.hpp> #include <boost/property_tree/json_parser.hpp> using namespace std; using namespace boost; using namespace boost::property_tree; // json_parse.cpp : 定义控制台应用程序的入口点。 // string generate_sample_json_string() { string json_string = "{\"programmers\":[{ \"firstName\": \"Brett\", \"lastName\":\"McLaughlin\", \"email\": \"aaaa\" },\ { \"firstName\": \"Jason\", \"lastName\":\"Hunter\", \"email\": \"bbbb\" }]}"; return json_string; } class people { public: people(){}; people(const string& strFirstName,const string& strLastName):firstname(strFirstName),lastname(strLastName){} string get_firstname() {return firstname;} void set_firstname(const string& firstname){ this->firstname = firstname;} string get_lastname() {return lastname;} void set_lastname(const string& lastname) { this->lastname = lastname;} private: string firstname; string lastname; }; class programmer:public people { public: programmer(){}; programmer(const string& strFirstName,const string& strLastName,const string& strEmail):people(strFirstName,strLastName),email(strEmail){} string get_email() {return email;} void set_email(const string& email) {this->email = email;} private: string email; }; typedef vector<people> PEOPLELIST; int _tmain(int argc, _TCHAR* argv[]) { try { string json_string = generate_sample_json_string(); cout<<json_string<<endl; PEOPLELIST plist; ptree jsonparser; stringstream jsonstream(json_string); json_parser::read_json<ptree>(jsonstream,jsonparser); int count = jsonparser.count("programmers"); // for (ptree::iterator it = jsonparser.begin(); it != jsonparser.end(); ++it) // { // ; // } ptree newprogrammer,proot,newprogrammers; newprogrammer.put<string>("firstName","dooo"); newprogrammer.put<string>("lastName","dxxx"); newprogrammer.put<string>("email","2345"); newprogrammers.push_back(make_pair("",newprogrammer)); newprogrammers.push_back(make_pair("",newprogrammer)); // jsonparser.add_child("programmers",newprogrammers); // jsonparser.put_child("programmers",newprogrammers);
/*
ptParse.get_child("").put_value();
ptree::iterator it = ptParse.begin(); ptree p1; for (;it!=ptParse.end();++it) { string strkey = it->first; p1 = it->second; string strdata = p1.data();
}
*/ ptree endpro = jsonparser.get_child("programmers"); endpro.push_back(make_pair("",newprogrammer)); jsonparser.put_child("programmers",endpro); stringstream stroutstream; json_parser::write_json(stroutstream,jsonparser); string str; stroutstream>>str; cout<<"************************"<<endl; cout<<stroutstream.str(); BOOST_FOREACH(boost::property_tree::ptree::value_type &value,jsonparser.get_child("programmers")) { ptree& childparse = value.second; plist.push_back(programmer(childparse.get<string>("firstName"), childparse.get<string>("lastName"), childparse.get<string>("email"))); } } catch(json_parser::json_parser_error& error) { cout<<error.message()<<endl; } catch(...) { } getchar(); return 0; } //一下是一个比较通用的json值得获取 template<typename T> T GetJsonValue(const std::string& jsonstr,const std::string& jsonkey,T defaultvalue = T()) { T retvalue = defaultvalue; try { ptree ptParse; std::stringstream jsonStream(jsonstr); json_parser::read_json<ptree>(jsonStream,ptParse); retvalue = ptParse.get<T>(jsonkey,defaultvalue); }catch(...) { } return retvalue; } //输出json string class remove_ch { private: std::string removetoken; public: remove_ch():removetoken(" \n\t\r"){}; remove_ch(const std::string& strtoken):removetoken(strtoken){}; bool operator()(char item) { return std::string::npos != removetoken.find_first_of(item); } }; inline std::string remove_chars(std::string & strSrc,const std::string strChars=" \n\t\r") { strSrc.erase(remove_if(strSrc.begin(),strSrc.end(),remove_ch(strChars)),strSrc.end()); return strSrc; } inline std::string JsonSerializeToString(const boost::property_tree::ptree& jsonptree) { std::string jsonstring(""); try{ std::stringstream jsonstream; boost::property_tree::json_parser::write_json(jsonstream,jsonptree); jsonstring = jsonstream.str(); remove_chars(jsonstring); //jsonstring.erase(remove_if(jsonstring.begin(),jsonstring.end(),remove_ch()),jsonstring.end()); } catch(...){ } return jsonstring; }
boost库json_parser使用
最新推荐文章于 2024-08-21 09:14:38 发布