boost库json_parser使用

#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;
}






以下是操作 JSONBoost 接口: 1. `boost::property_tree::ptree` 类:用于表示 JSON 树形结构的类,可以读取和写入 JSON 数据。 2. `boost::property_tree::read_json` 函数:用于从文件或字符串中读取 JSON 数据并将其转换为 `ptree` 对象。 3. `boost::property_tree::write_json` 函数:用于将 `ptree` 对象中的 JSON 数据写入文件或字符串。 4. `boost::property_tree::json_parser::read_json` 函数:与 `read_json` 函数类似,但是它可以读取 JSON 数据并将其转换为任意数据结构。 5. `boost::property_tree::json_parser::write_json` 函数:与 `write_json` 函数类似,但是它可以将任意数据结构转换为 JSON 数据并写入文件或字符串。 6. `boost::property_tree::json_parser::parse` 函数:用于解析 JSON 数据并返回一个 `ptree` 对象。 7. `boost::property_tree::json_parser::generate` 函数:用于将 `ptree` 对象转换为 JSON 数据并返回一个字符串。 8. `boost::property_tree::json_parser::write` 函数:用于将 `ptree` 对象中的 JSON 数据写入文件。 9. `boost::property_tree::json_parser::read` 函数:用于从文件中读取 JSON 数据并将其转换为 `ptree` 对象。 10. `boost::property_tree::json_parser::write_json_narrow` 函数:与 `write_json` 函数类似,但是它将 UTF-8 字符串写入文件或字符串。 11. `boost::property_tree::json_parser::read_json_narrow` 函数:与 `read_json` 函数类似,但是它从一个 UTF-8 字符串中读取 JSON 数据。 12. `boost::property_tree::json_parser::write_json_internal` 函数:与 `write_json` 函数类似,但是它将 JSON 数据写入文件或字符串。 13. `boost::property_tree::json_parser::read_json_internal` 函数:与 `read_json` 函数类似,但是它从一个字符串中读取 JSON 数据。 14. `boost::property_tree::json_parser::write_json_internal_ordered` 函数:与 `write_json` 函数类似,但是它将 JSON 数据写入文件或字符串,并按照键的顺序排序。 15. `boost::property_tree::json_parser::read_json_internal_ordered` 函数:与 `read_json` 函数类似,但是它从一个字符串中读取 JSON 数据,并按照键的顺序排序。 16. `boost::property_tree::json_parser::json_writer_settings` 类:用于配置 `write_json` 函数的输出格式。 17. `boost::property_tree::json_parser::json_reader_settings` 类:用于配置 `read_json` 函数的输入格式。 18. `boost::property_tree::json_parser::json_parser_error` 类:表示 JSON 解析错误的异常类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值