4.8 C++ Boost 应用JSON解析库
property_tree 是 Boost 库中的一个头文件库,用于处理和解析基于 XML、Json 或者 INFO 格式的数据。 property_tree 可以提供一个轻量级的、灵活的、基于二叉数的通用容器,可以处理包括简单值(如 int、float)和复杂数据结构(如结构体和嵌套容器)在内的各种数据类型。它可以解析数据文件到内存中,然后通过迭代器访问它们。
在 Boost 库中,property_tree 通常与 boost/property_tree/xml_parser.hpp、boost/property_tree/json_parser.hpp 或 boost/property_tree/info_parser.hpp 文件一起使用。这些文件分别提供了将 XML、JSON 或 INFO 格式数据解析为 property_tree 结构的功能。
首先我们需要自行创建一个测试config.json
文件,后期的所有案例演示及应用都需要这个库的支持。
{
"username": "lyshark",
"age": 24,
"get_dict": { "username": "lyshark" },
"get_list": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
"user_data":
[
[ "192.168.1.1", "root", "123456" ],
[ "192.168.1.2", "admin", "123456" ],
[ "192.168.1.3", "mysql", "123456" ]
],
"user_dict":
[
{ "uid": 1001, "uname": "admin" },
{ "uid": 1002, "uname": "lyshark" },
{ "uid": 1003, "uname": "root" }
],
"get_root":
[
{
"firstName": "admin",
"lastName": "JackWang",
"email": "admin@lyshark.com"
},
{
"firstName": "lyshark",
"lastName": "xusong",
"email": "me@lyshark.com"
}
],
"get_my_list":
{
"get_uint": "[1,2,3,4,5,6,7,8,9,0]",
"get_string": "['admin','lyshark','root']"
}
}
8.1 解析单个节点
代码中使用了 Boost C++ 库中的 property_tree 和 json_parser 来解析 JSON 文件。它的主要功能是读取指定路径下的 "c://config.json" 文件,然后获取其中的用户名和年龄信息(如果存在的话),并将它们输出到控制台。
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace std;
int main(int argc, char* argv[])
{
boost::property_tree::ptree ptr;
boost::property_tree::read_json("c://config.json", ptr);
cout << "是否存在: " << ptr.count("username") << endl;
if (ptr.count("username") != 0)
{
std::string username = ptr.get<std::string>("username");
std::cout << "用户名: " << username << std::endl;
}
if (ptr.count("age") != 0)
{
int age = ptr.get<int>("age");
std::cout << "年龄: " << age << std::endl;
}
system("pause");
return 0;
}
8.2 解析单个列表
代码中使用了 Boost C++ 库中的 property_tree 和 json_parser 来解析 JSON 文件。它的功能是读取指定路径下的 "c://config.json" 文件,并提取名为 "get_list" 的字段的值,并将其输出到控制台。
#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace std;
void GetJson(std::string &filePath)
{
boost::property_tree::ptree ptr;
std::vector<std::string> item;
boost::property_tree::read_json(filePath, ptr);
// 先判断是否存在字段
if (ptr.count("get_list") == 1)
{
boost::property_tree::ptree pChild = ptr.get_child("get_list");
for (auto pos = pChild.begin(); pos != pChild.end(); ++pos)
{
// 迭代循环,将元素房补vector列表中
std::string list = pos->second.get_value<string>();
item.push_back(list);
}
}
// 迭代输出vector中的数据
for (auto x = item.begin(); x != item.end(); ++x)
{
cout << *x << endl;
}
}
int main(int argc, char* argv[])
{
GetJson(std::string("c://config.json"));
system("pause");
return 0;
}
8.3 解析嵌套列表
这段代码依然使用了 Boost C++ 库中的 property_tree 和 json_parser 来解析 JSON 文件。它的功能是读取指定路径下的 "c://config.json" 文件,并提取名为 "user_data" 的字段的第二列数据,并将其输出到控制台。
#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace std;
void GetJson(std::string &filePath)
{
boost::property_tree::ptree ptr;
boost::property_tree::read_json(filePath, ptr);
std::vector<std::string> item;
for (auto& root_child : ptr.get_child("user_data"))
{
int count = 1;
for (auto& x : root_child.second)
{
// count 用于指定需要那一列的记录
if (count == 2)
{
std::string sub_data = x.second.get_value<std::string>();
cout << "输出元素: " << sub_data << endl;
item.push_back(sub_data);
}
count++;
}
}
// 迭代输出vector中的数据
for (auto x = item.begin(); x != item.end(); ++x)
{
cout << *x << endl;
}
}
int main(int argc, char* argv[])
{
GetJson(std::string("c://config.json"));
system("pause");
return 0;
}
8.4 解析多层字典
代码同样使用了 Boost C++ 库中的 property_tree 和 json_parser 来解析 JSON 文件。它的功能是读取指定路径下的 "c://config.json" 文件,并提取名为 "get_dict" 和 "user_dict" 的字段数据,并将其输出到控制台。
#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace std;
void GetJson(std::string &filePath)
{
boost::property_tree::ptree ptr;
boost::property_tree::read_json(filePath, ptr);
// 解析单一字典
std::string username = ptr.get_child("get_dict").get<std::string>("username");
cout << "姓名: " << username << endl;
// 解析多层字典
boost::property_tree::ptree root_ptr, item;
root_ptr = ptr.get_child("user_dict");
// 输出第二层
for (boost::property_tree::ptree::iterator it = root_ptr.begin(); it != root_ptr.end(); ++it)
{
item = it->second;
cout << "UID: " << item.get<int>("uid") << " 姓名: " << item.get<string>("uname") << endl;
}
}
int main(int argc, char* argv[])
{
GetJson(std::string("c://config.json"));
system("pause");
return 0;
}
第二种方式,通过多次迭代解析多层字典,并将字典中的特定value放入到vector容器内。
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace std;
using namespace boost;
using namespace boost::property_tree;
int main(int argc, char *argv[])
{
boost::property_tree::ptree ptr;
boost::property_tree::read_json("C://config.json", ptr);
// 判断是否存在get_root
if (ptr.count("get_root") == 1)
{
std::vector<string> vecStr;
std::string get_first_name;
boost::property_tree::ptree p1, p2;
// 读取到根节点
p1 = ptr.get_child("get_root");
// 循环枚举
for (ptree::iterator it = p1.begin(); it != p1.end(); ++it)
{
// 获取到字典的value值
p2 = it->second;
get_first_name = p2.get<string>("firstName");
// 将获取到的value转换为vector容器
vecStr.push_back(get_first_name);
}
// 输出容器中的内容
for (int x = 0; x < vecStr.size(); x++)
{
std::cout << vecStr[x] << std::endl;
}
}
std::system("pause");
return 0;
}
8.5 写出JSON文件
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace std;
// 初始化字符串
bool InitJSON()
{
string str = "{\"uuid\":1001,\"Student\":[{\"Name\":\"admin\"},{\"Name\":\"lyshark\"}]}";
stringstream stream(str);
boost::property_tree::ptree strTree;
try{
read_json(stream, strTree);
}
catch (boost::property_tree::ptree_error & e) {
return false;
}
write_json("c://config.json", strTree);
return true;
}
// 初始化列表
void InitArray()
{
boost::property_tree::ptree ptr;
boost::property_tree::ptree children;
boost::property_tree::ptree child1, child2, child3;
child1.put("", 1);
child2.put("", 2);
child3.put("", 3);
children.push_back(std::make_pair("", child1));
children.push_back(std::make_pair("", child2));
children.push_back(std::make_pair("", child3));
ptr.add_child("MyArray", children);
write_json("c://Array.json", ptr);
}
// 初始化字典
void InitDict()
{
boost::property_tree::ptree ptr;
boost::property_tree::ptree children;
boost::property_tree::ptree child1, child2, child3;
child1.put("childkeyA", 1);
child1.put("childkeyB", 2);
child2.put("childkeyA", 3);
child2.put("childkeyB", 4);
child3.put("childkeyA", 5);
child3.put("childkeyB", 6);
children.push_back(std::make_pair("", child1));
children.push_back(std::make_pair("", child2));
children.push_back(std::make_pair("", child3));
ptr.put("testkey", "testvalue");
ptr.add_child("MyArray", children);
write_json("c://MyDict.json", ptr);
}
int main(int argc, char* argv[])
{
InitArray();
InitDict();
InitJSON();
boost::property_tree::ptree ptr;
boost::property_tree::read_json("c://config.json", ptr);
// 修改uuid字段
if (ptr.count("uuid") != 0)
{
ptr.put("uuid", 10002);
}
boost::property_tree::write_json("c://config.json", ptr);
system("pause");
return 0;
}
8.6 自定义结构解析
#include <iostream>
#include <string>
#include <vector>
#include <boost/assign.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using namespace std;
using namespace boost;
using namespace boost::property_tree;
// 将整数转为int容器
std::vector<int> JsonIntToVector(std::string string_ptr)
{
std::vector<std::string> vect;
// 去掉里面的[]符号
std::string item = boost::trim_copy_if(string_ptr, (boost::is_any_of("[") || boost::is_any_of("]")));
boost::split(vect, item, boost::is_any_of(",,"), boost::token_compress_on);
std::vector<int> ref;
for (auto each : vect)
{
ref.push_back(boost::lexical_cast<int>(each));
}
return ref;
}
// 将字符串列表转为string容器
std::vector<std::string> JsonStringToVector(std::string string_ptr)
{
std::vector<std::string> ref;
std::vector<std::string> vect;
std::string item = boost::trim_copy_if(string_ptr, (boost::is_any_of("[") || boost::is_any_of("]")));
boost::split(vect, item, boost::is_any_of(",,"), boost::token_compress_on);
for (auto each : vect)
{
// 去掉单引号
std::string ea = boost::trim_copy_if(each, (boost::is_any_of("'") || boost::is_any_of("\"")));
ref.push_back(ea);
}
return ref;
}
int main(int argc, char *argv[])
{
boost::property_tree::ptree ptr;
boost::property_tree::read_json("./config.json", ptr);
// 输出特殊的整数
std::string ustring = ptr.get_child("get_my_list").get<std::string>("get_uint");
std::vector<int> ref_int = JsonIntToVector(ustring);
for (int x = 0; x < ref_int.size(); x++)
{
std::cout << "输出整数: " << ref_int[x] << std::endl;
}
// 输出特殊的字符串
std::string ustring1 = ptr.get_child("get_my_list").get<std::string>("get_string");
std::vector<std::string> ref_string = JsonStringToVector(ustring1);
for (int x = 0; x < ref_string.size(); x++)
{
std::cout << "输出字符串: " << ref_string[x] << std::endl;
}
std::system("pause");
return 0;
}
4.8 C++ Boost 应用JSON解析库的更多相关文章
- Boost.JSON Boost的JSON解析库(1.75首发)
目录 目录 Boost的1.75版本新库 JSON库简介 JSON的简单使用 编码 最通用的方法 使用std::initializer_list json对象的输出 两种对比 解码 简单的解码 增加错 ...
- C++的Json解析库:jsoncpp和boost
C++的Json解析库:jsoncpp和boost - hzyong_c的专栏 - 博客频道 - CSDN.NET C++的Json解析库:jsoncpp和boost 分类: 网络编程 开源库 201 ...
- C++的Json解析库:jsoncpp和boost(转)
原文转自 http://blog.csdn.net/hzyong_c/article/details/7163589 JSON(JavaScript Object Notation)跟xml一样也是一 ...
- [转]C++的Json解析库:jsoncpp和boost
JSON(JavaScript Object Notation)跟xml一样也是一种数据交换格式,了解json请参考其官网http://json.org,本文不再对json做介绍,将重点介绍c++的j ...
- Tomjson - 一个"短小精悍"的 json 解析库
Tomjson,一个"短小精悍"的 json 解析库,tomjson使用Java语言编写,主要作用是把Java对象(JavaBean)序列化为json格式字符串,将json格式字符 ...
- fastjson是阿里巴巴的开源JSON解析库
fastjson的API十分简洁. String text = JSON.toJSONString(obj); //序列化 VO vo = JSON.parseObject("{...}&q ...
- python 中的json解析库
当一个json 数据很大的时候.load起来是很耗时的.python中常见的json解析库有cjson,simplesjson,json, 初步比较了一下, 对于loads来讲 simplejson ...
- Tomjson - json 解析库
Tomjson - 一个"短小精悍"的 json 解析库 Tomjson,一个"短小精悍"的 json 解析库,tomjson使用Java语言编写,主要作用是把 ...
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...
- 常用json解析库比较及选择 fastjson & gson
一.常用json解析库比较及选择 1.简介 fastjson和gson是目前比较常用的json解析库,并且现在我们项目代码中,也在使用这两个解析库. fastjson 是由阿里开发的,号称是处理jso ...
随机推荐
- 【LibCurl】C++使用libcurl实现HTTP POST和GET、PUT
libcurl简介 libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcurl同样支持HT ...
- CH0304 IncDec Sequence (差分)
题目链接: https://ac.nowcoder.com/acm/contest/999/B 思路:(见图中解释) AC代码: #include<bits/stdc++.h> using ...
- 6.0 《数据库系统概论》之关系数据库的规范化理论(数据依赖对表的影响[插入-删除-修改-冗余]、1NF-2NF-3NF-BCNF-4NF、函数依赖与多值依赖)
前言 本篇文章学习书籍:<数据库系统概论>第5版 王珊 萨师煊编著 视频资源来自:数据库系统概论完整版(基础篇+高级篇+新技术篇) 由于 BitHachi 学长已经系统的整理过本书了,我在 ...
- AtCoder Beginner Contest 187 题解
A - Large Digits 按要求求出两个数的每位之和,进行比较即可. 时间复杂度 \(\mathcal{O}(\log(AB))\). B - Gentle Pairs 枚举所有点对求斜率. ...
- 智能制造之 SMT 产线监控管理可视化
前言 随着<中国制造2025>的提出,制造业迎来了全新的发展机遇.更多的企业将制造业信息化技术进行广泛的应用,如 MES 系统.数字孪生以及生产管理可视化等技术的研究应用,已经成为社会各界 ...
- 基于函数计算自定义运行时快速部署一个 springboot 项目 什么是函数计算?
什么是函数计算? 函数计算是事件驱动的全托管计算服务.使用函数计算,您无需采购与管理服务器等基础设施,只需编写并上传代码.函数计算为您准备好计算资源,弹性地可靠地运行任务,并提供日志查询.性能监控和报 ...
- Idea 进行远程服务器debug操作
本文为博主原创,转载请注明出处: 很多时候为了定位服务器的问题,不方便定位时,采用idea 远程debug 服务器环境的服务进行问题定位,主要操作步骤如下: 1. 修改服务器服务的JVM 配置,开启远 ...
- idea新建spring boot 项目右键无package及java类的选项
新创建的spring boot项目,只有一个默认的资源目录及启动配置. 在 group 的目录下右键新建包路径时 ,发现没有package选项,也没有Java Class的选项: 解决办法: File ...
- STM32CubeMX教程23 FSMC - IS62WV51216(SRAM)驱动
1.准备材料 开发板(正点原子stm32f407探索者开发板V2.4) STM32CubeMX软件(Version 6.10.0) 野火DAP仿真器 keil µVision5 IDE(MDK-Arm ...
- [转帖]使用 TiUP 部署 TiDB 集群
https://docs.pingcap.com/zh/tidb/stable/production-deployment-using-tiup TiUP 是 TiDB 4.0 版本引入的集群运维工具 ...