boost生成和解析json的完整例子

//
//  json_parser.h
//  mongoose
//
//  Created by Alex on 5/26/14.
//  Copyright (c) 2014 Cenbong. All rights reserved.
//

#ifndef __mongoose__json_parser__
#define __mongoose__json_parser__
#include <string>
#include <sstream>
#include <vector>
class sms_info
{
	sms_info()
	{
		index_ = 0;
	}
public:
	static std::string INDEX;
	static std::string TO;
	static std::string FROM;
	static std::string MSG;
	static std::string SPLITTER;
	static std::string TAG;
private:
	int index_;
	std::string to_;
	std::string from_;
	std::string msg_;
	
public:
	sms_info(int index, const std::string& to, const std::string& from, const std::string& msg)
	{
		index_ = index;
		to_ = to;
		from_ = from;
		msg_ = msg;
	}
	int index()
	{
		return index_;
	}
	std::string to()
	{
		return to_;
	}
	std::string from()
	{
		return from_;
	}
	std::string msg()
	{
		return msg_;
	}
};

class json_parser
{
private:
	static std::string ROOTNAME;
public:
	static std::string generate(const std::vector<sms_info>& smss);
	static bool parse(const std::string& s, std::vector<sms_info>& smss);
public:
	static void tester();
};
#endif /* defined(__mongoose__json_parser__) */


//
//  json_parser.cpp
//  mongoose
//
//  Created by Alex on 5/26/14.
//  Copyright (c) 2014 Cenbong. All rights reserved.
//

#include "json_parser.h"
#include <boost/progress.hpp>
#include "sstream"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/foreach.hpp>
std::string sms_info::INDEX = "index";
std::string sms_info::TO = "to";
std::string sms_info::FROM = "from";
std::string sms_info::MSG = "msg";
std::string sms_info::SPLITTER = ",";
std::string sms_info::TAG = "SMSInfo";

std::string json_parser::ROOTNAME = "smss";
void json_parser::tester()
{
	std::vector<sms_info> smss1;
	for(int i = 0; i < 5; i++)
	{
		int index = i;
		std::string to = "1860000" ;
		std::string from = "1880000" ;
		std::string msg = "这个短信发给Alex,   谢谢。 ";
		smss1.push_back(sms_info(index, to, from, msg));
	}
	std::string s = generate(smss1);
	std::vector<sms_info> smss2;
	parse(s, smss2);
	assert(smss1.size() == smss2.size());
}
std::string json_parser::generate(const std::vector<sms_info>& smss)
{
	boost::property_tree::ptree pt_root;
	boost::property_tree::ptree children;
	boost::property_tree::ptree child;
	
	for(size_t i = 0; i < smss.size(); i++)
	{
		sms_info sms = smss.at(i);
		child.put(sms_info::INDEX, sms.index());
		child.put(sms_info::TO, sms.to());
		child.put(sms_info::FROM, sms.from());
		child.put(sms_info::MSG, sms.msg());
		children.push_back(std::make_pair("", child));
	}
	
	pt_root.add_child(ROOTNAME, children);
	std::stringstream ss;
	boost::property_tree::write_json(ss, pt_root);
	std::string s = ss.str();
	return s;
}
bool json_parser::parse(const std::string& s, std::vector<sms_info>& smss)
{
	std::istringstream iss;
	iss.str(s.c_str());
	boost::property_tree::ptree parser;
	boost::property_tree::json_parser::read_json(iss, parser);
	boost::property_tree::ptree sms_array = parser.get_child(ROOTNAME);
	BOOST_FOREACH(boost::property_tree::ptree::value_type &v, sms_array)
    {
		boost::property_tree::ptree p = v.second;
		int index = p.get<int>(sms_info::INDEX);
		std::string to = p.get<std::string>(sms_info::TO);
		std::string from = p.get<std::string>(sms_info::FROM);
		std::string msg = p.get<std::string>(sms_info::MSG);
		
		smss.push_back(sms_info(index, to, from, msg));
    }
	return true;
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值