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

Posted 炖丸子的猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用boost库完成读写JSON字符串相关的知识,希望对你有一定的参考价值。

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字符串的主要内容,如果未能解决你的问题,请参考以下文章

使用 Boost.Threads 的读写锁(如何转换这个简单的类)

使用 boost 库编写多线程程序时出错

使用 boost 或标准库的 wchar 参数

当 read_json 在多个线程中使用而不与 boost.thread 库链接时,我们是不是可以始终使用 BOOST_SPIRIT_THREADSAFE 标志?

C++的Json解析库:jsoncpp和boost(转)

boost::asio 学习