长风破浪会有时,直挂云帆济沧海

Dream Word

博客园 首页 新随笔 联系 订阅 管理

json序列化

  

#ifndef FND_JSON_OBJECT_H
#define FND_JSON_OBJECT_H

#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <string>

using ptree = boost::property_tree::ptree;
inline std::string ptreeToJsonString(const ptree& tree)
{
        std::stringstream ss;
        boost::property_tree::write_json(ss, tree, false);
        return ss.str();
}
#endif

int main()
{
        //解析
        ptree tree;

        std::stringstream ss(std::string(***,));
        boost::property_tree::read_json(ss,tree);

        std::cout<<"client: '";
        std::cout<<tree.get<std::string>("name");
        std::cout<<"'says'";
        std::cout<<tree.get<std::string>("information");
        std::cout<<"'\n";

        //生成json
        ptree tree;
        tree.push("name",name);
        std::string outbuffer = ptreeToJsonString(tree);
}

  

posted on 2018-07-31 16:40  长风II  阅读(524)  评论(0编辑  收藏  举报