码迷,mamicode.com
首页 > Web开发 > 详细

使用boost库完成读写JSON字符串

时间:2020-01-17 15:04:43      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:cout   org   property   air   data   stream   his   代码   https   

boost库的编译不做赘述,这里用的版本是1.69.0,其他版本应该差不多

boost官网:https://www.boost.org/

0.前言

今天要整的JSON是这样的

{
    "description": "this is a JSON test",
    "version": "100",
    "list": {
        "listkey1": "listvalue1",
        "listkey2": "listvalue2",
        "listkey3": "listvalue3"
    },
    "words": [
        "apple",
        "banana",
        "cat"
    ]
}

 

首先先新建一个vs的工程,设置include路径和lib路径

额外添加的lib为\boost\1.69.0\stage64\lib\libboost_system-vc141-mt-gd-x64-1_69.lib

 

在头文件中添加

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

使用ptree来完成JSON的读写操作

 

1、写JSON字符串

1.0、如何将ptree转换成字符串输出到屏幕

使用stringstream

    std::stringstream ss;
    boost::property_tree::write_json(ss, pt);
    std::string strContent = ss.str();    

 

1.1、添加简单值

   //    root
    boost::property_tree::ptree root;

    //    add simple value
    root.put(std::string("description"), std::string("this is a JSON test"));
    root.put(std::string("version"), 100);

 

1.2、添加一组对象

    //    add a list of objs
    boost::property_tree::ptree ptObjs;
    ptObjs.put(std::string("listkey1"), std::string("listvalue1"));
    ptObjs.put(std::string("listkey2"), std::string("listvalue2"));
    ptObjs.put(std::string("listkey3"), std::string("listvalue3"));
    root.add_child(std::string("list"), ptObjs);

 

1.3、添加一组值

    //    add a list of values
    boost::property_tree::ptree ptValItem1, ptValItem2, ptValItem3, ptVals;
    ptValItem1.put_value(std::string("apple"));
    ptVals.push_back(std::make_pair("", ptValItem1));
    ptValItem2.put_value(std::string("banana"));
    ptVals.push_back(std::make_pair("", ptValItem2));
    ptValItem3.put_value(std::string("cat"));
    ptVals.push_back(std::make_pair("", ptValItem3));
    root.add_child(std::string("words"), ptVals);

 

1.4、输出到屏幕

先通过1.0写一个输出的function

void ptreePrint(boost::property_tree::ptree pt, std::string ptName, std::string descritpion)
{
    std::stringstream ss;
    boost::property_tree::write_json(ss, pt);
    std::string strContent = ss.str();

    std::cout << descritpion << std::endl << ptName << " : " << strContent << std::endl << "--------------------------------------\n";
}

然后将每步产生的ptree输出

技术图片

 

2.读JSON字符串

2.1 读简单值

    //    read simple value
    std::string description = pt.get<std::string>("description");
    int version = pt.get<int>("version");

 

2.2 读一组对象

    //    read list of objs
    std::cout << std::endl << "read list of objs " << std::endl;

    for (auto ptItem : pt.get_child("list"))
    {
        std::string key = ptItem.first;
        std::string val = ptItem.second.data();

        std::cout << "key:\t" << key << "\tvalue:\t" << val << std::endl;
    }

 

2.3 读一组值

    //    read list of values 
    std::cout << std::endl << "read list of values" << std::endl << "words: ";
    for (auto ptItem : pt.get_child("words"))
    {
        std::cout << ptItem.second.data() << " ";
    }

 

2.4 输出到屏幕

技术图片

 

3.完整代码

#include "pch.h"
#include <iostream>

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

void readTest(boost::property_tree::ptree pt)
{
    std::cout << __func__ << std::endl;
    
    //    read simple value
    std::string description = pt.get<std::string>("description");
    int version = pt.get<int>("version");

    std::cout << "description : " << description << std::endl << "version : " << version << std::endl;

    //    read list of objs
    std::cout << std::endl << "read list of objs " << std::endl;

    for (auto ptItem : pt.get_child("list"))
    {
        std::string key = ptItem.first;
        std::string val = ptItem.second.data();

        std::cout << "key:\t" << key << "\tvalue:\t" << val << std::endl;
    }

    //    read list of values 
    std::cout << std::endl << "read list of values" << std::endl << "words: ";
    for (auto ptItem : pt.get_child("words"))
    {
        std::cout << ptItem.second.data() << " ";
    }
}

void ptreePrint(boost::property_tree::ptree pt, std::string ptName, std::string descritpion)
{
    std::stringstream ss;
    boost::property_tree::write_json(ss, pt);
    std::string strContent = ss.str();

    std::cout << descritpion << std::endl << ptName << " : " << strContent << std::endl << "--------------------------------------\n";
}

void JSONtest()
{
    //    root
    boost::property_tree::ptree root;

    //    add simple value
    root.put(std::string("description"), std::string("this is a JSON test"));
    root.put(std::string("version"), 100);

    ptreePrint(root, std::string("root"), "add simple value");

    //    add a list of objs
    boost::property_tree::ptree ptObjs;
    ptObjs.put(std::string("listkey1"), std::string("listvalue1"));
    ptObjs.put(std::string("listkey2"), std::string("listvalue2"));
    ptObjs.put(std::string("listkey3"), std::string("listvalue3"));
    root.add_child(std::string("list"), ptObjs);

    ptreePrint(root, std::string("root"), "add a list of objs");

    //    add a list of values
    boost::property_tree::ptree ptValItem1, ptValItem2, ptValItem3, ptVals;
    ptValItem1.put_value(std::string("apple"));
    ptVals.push_back(std::make_pair("", ptValItem1));
    ptValItem2.put_value(std::string("banana"));
    ptVals.push_back(std::make_pair("", ptValItem2));
    ptValItem3.put_value(std::string("cat"));
    ptVals.push_back(std::make_pair("", ptValItem3));
    root.add_child(std::string("words"), ptVals);
    ptreePrint(ptVals, std::string("ptVals"), "");
    ptreePrint(root, std::string("root"), "add a list of values");

    readTest(root);


}

int main()
{
    JSONtest();

}

使用boost库完成读写JSON字符串

标签:cout   org   property   air   data   stream   his   代码   https   

原文地址:https://www.cnblogs.com/Sseakompp/p/12205793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!