C++ boost 解析 Json

property_tree可以解析ini,xml,json,info等格式的文本
以下示例是解析json格式的文本

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <time.h>

using namespace std;
using namespace boost::property_tree;


const string file_path = "c:\\test.txt";

void write_json_data_into_file(void)
{
    printf("%s\n","write json data");

    boost::property_tree::ptree root, items;

    boost::property_tree::ptree item1;
    item1.put("ID","1");
    item1.put("Name","wang");
    items.push_back(std::make_pair("1",item1));

    boost::property_tree::ptree item2;
    item2.put("ID","2");
    item2.put("Name","zhang");
    items.push_back(std::make_pair("2",item2));

    root.put_child("users",items);
    boost::property_tree::write_json(file_path,root);
}

void read_json_data_from_file(void)
{
    printf("%s\n","read json data");
    boost::property_tree::ptree root;
    boost::property_tree::ptree items;
    boost::property_tree::read_json<boost::property_tree::ptree>(file_path,root);

    items = root.get_child("users");
    for(boost::property_tree::ptree::iterator it=items.begin(); it != items.end(); ++it)
    {
        string key=it->first;//key ID
        string ID=it->second.get<string>("ID");
        string Name=it->second.get<string>("Name");
        cout<<"key: "<<key.c_str()<<'\t';
        printf("ID: %s    Name: %s",ID.c_str(), Name.c_str());
        cout<<endl;
        cout<<"ID:"<<ID<<'\t'<<"Name:"<<Name<<endl;
    }
    cout<<"success"<<endl;
}

void write_json_data_into_string(void)
{
    boost::property_tree::ptree item;
    item.put("a","2");
    std::stringstream is;
    boost::property_tree::write_json(is,item);
    std::string s = is.str();
    cout<<"json s:"<<s<<endl;
}

void read_json_data_from_string(void)
{
    /*
        C++ 中 字符串形式的json串 需要使用 \ 转义 双引号
    */
    std::string str_json = "{\"count\":10,\"people\":[{ \"firstName\": \"Brett\", \"lastName\":\"McLaughlin\", \"email\": \"aaaa\" },{ \"firstName\": \"Jason\", \"lastName\":\"Hunter\", \"email\": \"bbbb\"},{ \"firstName\": \"Elliotte\", \"lastName\":\"Harold\", \"email\": \"cccc\" }]}";
    std::stringstream str_stream(str_json);
    boost::property_tree::ptree root;
    boost::property_tree::read_json(str_stream,root);
    root.put("upid","001");

    // 插入一个数组
    boost::property_tree::ptree exif_array;
    boost::property_tree::ptree array1, array2, array3;
    array1.put("Make", "NIKON");
    array2.put("DateTime", "2011:05:31 06:47:09");
    array3.put("Software", "Ver.1.01");

    //   exif_array.push_back(std::make_pair("Make", "NIKON"));
    //   exif_array.push_back(std::make_pair("DateTime", "2011:05:31 06:47:09"));
    //   exif_array.push_back(std::make_pair("Software", "Ver.1.01"));

    exif_array.push_back(std::make_pair("", array1));
    exif_array.push_back(std::make_pair("", array2));
    exif_array.push_back(std::make_pair("", array3));

    root.put_child("exifs", exif_array);
    std::stringstream str_stream_temp;
    boost::property_tree::write_json(str_stream_temp, root);
    //write_json(str_stream_temp, root);
    std::string str = str_stream_temp.str();
    cout<<str<<endl;
}


void testMsg(void)
{
    string str_json = "{\"TEST\":\"\",\"MSG\":\"130\",\"TKT\":[{\"DTYP\":\"T\",\"STOT\":\"1\",\"SNUM\":\"1\",\"CPN\":[{\"CNBR\":\"1\",\"DDAT\":\"090117\",\"DTME\":\"1205\",\"ADAT\":\"090117\",\"ATME\":\"1340\",\"ORIG\":\"TSA\",\"DEST\":\"PVG\",\"ALC1\":\"FM\",\"FLTN\":\"802\",\"CLAS\":\"Y\",\"FSTN\":\"OK\",\"FBAS\":\"Y\",\"BAGA\":\"20K\"}]}]}";
    std::stringstream str_stream(str_json);
    boost::property_tree::ptree root;
    boost
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值