C++ – 速通nlohmann json,nlohmann json使用教程
1 nlohmann json开发环境配置
nlohmann json是一个C++、开源的,只有单个头文件的、高效快速的json操作库。
优点:
- 现代C++风格,语法直观,使用简单
- 没有复杂的编译、依赖关系,直接包含头文件即可
缺点:
- 并不是最快的json库
- 内存占用稍大
Github仓库:https://github.com/nlohmann/json
因为nlohmann json支持单个头文件,所以在配置其开发环境时只需要下载其发布包include.zip
,解压缩该压缩包,然后将该文件下的子文件夹single_include
添加到项目中头文件路径即可。
使用时,在代码中包含其头文件
#include "nlohmann/json.hpp"
即可使用,非常简单。
2 nlohmann json的使用
2.1 json解析
2.1.1 json解析中的异常和容错处理
2.1.1.1 判断字符串是否可以被解析为json对象
在将字符串解析成json对象时,需要判断该字符串是可以被解析成json对象,我们可以使用以下代码
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
const std::string json_str = "niubi";
// 判断是否可以解析为json对象
if (!nlohmann::json::accept(json_str))
{
std::cerr << "parse json failed" << std::endl;
}
return 0;
}
也可以使用try...catch
捕捉解析异常,通过捕捉异常判断是否可以正常解析为json对象,如以下代码
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
const std::string json_str = "niubi";
try
{
nlohmann::json root = nlohmann::json::parse(json_str);
}
catch (nlohmann::json::parse_error& e)
{
std::cerr << "parse json failed, error message:"<< e.what() << std::endl;
}
return 0;
}
如果不想让nlohmann::json::parse
抛出异常,则可将
nlohmann::json root = nlohmann::json::parse(json_str);
修改为
nlohmann::json root = nlohmann::json::parse(json_str,nullptr,false);
这样就不会抛出异常了。
2.1.1.2 判断json对象是否包含键值
在解析json对象时,为了避免某个键值为空导致后续操作失败的问题,通常需要判断json是否包含某个键值,示例代码如下
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
const std::string json_str = "{\"data\":0}";
try
{
nlohmann::json root = nlohmann::json::parse(json_str,nullptr,false);
if (root.contains("data"))
{
int data_value = root["data"].get<int>();
std::cout << "parse success, the value data:" << data_value << std::endl;
}
else
{
std::cerr << "json not contains data key" << std::endl;
}
}
catch (nlohmann::json::parse_error& e)
{
std::cerr << "parse json failed, error message:"<< e.what() << std::endl;
}
return 0;
}
2.1.1.3 判断json对象键值对应值的数据类型
其实上一小节中的代码是有问题的,我直接使用
int data_value = root["data"].get<int>();
去获取data对应的int类型的值,如果data对应的不是int类型的话会出现错误,在不出现错误的情况下,解析出来的值也会不正确,所以应该先对data对应的值的数据类型进行判断,增强程序的健壮性,修改后的代码如下
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
const std::string json_str = "{\"data\":0}";
try
{
nlohmann::json root = nlohmann::json::parse(json_str,nullptr,false);
if (root.contains("data"))
{
if (root["data"].is_number_integer())
{
int data_value = root["data"].get<int>();
std::cout << "parse success, the value data:" << data_value << std::endl;
}
else
{
std::cerr << "key value is not int type" << std::endl;
}
}
else
{
std::cerr << "json not contains data key" << std::endl;
}
}
catch (nlohmann::json::parse_error& e)
{
std::cerr << "parse json failed, error message:"<< e.what() << std::endl;
}
return 0;
}
2.1.2 从文本文件中解析json
nlohmann json支持直接从文件对象中解析json,示例代码如下
#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"
int main()
{
const std::string file_path = "example.json";
std::ifstream file(file_path);
try
{
nlohmann::json root = nlohmann::json::parse(file);
}
catch (nlohmann::json::parse_error& e)
{
std::cerr << "parse json failed, error message:" << e.what() << std::endl;
}
return 0;
}
2.1.3 从字符串中解析json
nlohmann json支持直接从字符串中解析json,示例代码如下
#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"
int main()
{
const std::string json_str = "{\"data\":1,\"func_code\":10001}";
try
{
nlohmann::json root = nlohmann::json::parse(json_str);
}
catch (nlohmann::json::parse_error& e)
{
std::cerr << "parse json failed, error message:" << e.what() << std::endl;
}
return 0;
}
2.2 创建json对象
通过nlohmann json我们可以很简单就可以创建json对象,比如
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
nlohmann::json root;
root["pi"] = 3.141;
root["happy"] = true;
root["name"] = "Niels";
root["nothing"] = nullptr;
root["answer"]["everything"] = 42;
root["list"] = { 1, 0, 2 };
root["object"] = { {"currency", "USD"}, {"value", 42.99} };
return 0;
}
也可以这样
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
nlohmann::json root = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
return 0;
}
也可以从输入流中创建json
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
nlohmann::json root;
std::cin >> root;
return 0;
}
2.3 json对象序列化为字符串
nlohmann json支持将json对象序列化为字符串,示例代码如下
#include <iostream>
#include "nlohmann/json.hpp"
int main()
{
nlohmann::json root;
root["pi"] = 3.141;
root["happy"] = true;
root["name"] = "Niels";
root["nothing"] = nullptr;
root["answer"]["everything"] = 42;
root["list"] = { 1, 0, 2 };
root["object"] = { {"currency", "USD"}, {"value", 42.99} };
std::string json_str = root.dump();
std::cout << json_str << std::endl;
return 0;
}
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 速通nlohmann json,nlohmann json使用教程
原文链接:https://www.stubbornhuang.com/2731/
发布于:2023年07月20日 11:43:03
修改于:2023年07月20日 11:43:03
当前分类随机文章推荐
- CMake - 设置Debug或者Release编译模式 阅读4000次,点赞0次
- C++ - 对字符串和图片进行base64编解码 阅读1068次,点赞0次
- C++ - 跨平台在Windows、Linux系统上获取当前可执行程序路径 阅读60次,点赞0次
- C++ - Windows获取电脑上摄像头设备数目、名字以及id 阅读1097次,点赞0次
- C++ - 使用Websocket++编写客户端连接WebSocket服务器并进行通信 阅读5124次,点赞3次
- GCC/G++中编译优化选项-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -Oz各自的区别和作用 阅读5741次,点赞4次
- C++11 - 封装std::thread,增加子线程启动、暂停、唤起、停止功能 阅读5875次,点赞7次
- C++ - 阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点 阅读1613次,点赞0次
- C++ - 使用ffmpeg读取视频旋转角度并使用OpenCV根据旋转角度对视频进行旋转复原 阅读2297次,点赞0次
- CMake - Windows系统设置CMake代理 阅读491次,点赞0次
全站随机文章推荐
- 资源分享 - 交互式计算机图形学:基于WebGL的自顶向下方法(第七版),Interactive Computer Graphics - A top-down approach with WebGL(Seven 7th Edition)中文版PDF下载 阅读942次,点赞0次
- C++/OpenCV - 详解如何一步步将OpenCV的cv::Mat转换成深度学习模型推理所需的输入数据 阅读403次,点赞0次
- 怎么找CVPR历年会议全部论文? 阅读737次,点赞0次
- WordPress - 限制非管理员用户进入WordPress后台页面,重定向到首页 阅读1292次,点赞0次
- 深度学习 - 归纳轻量级神经网络(长期更新) 阅读424次,点赞0次
- 资源分享 - The NURBS Book (2nd,Les Pieg) 英文版PDF下载 阅读6826次,点赞3次
- C++ - std::vector初始化方式总结 阅读108次,点赞0次
- OpenCV/FFmpeg - 使用FFmpeg编码OpenCV中的BGR视频流为H264视频流以及解码H264视频流为OpenCV中的BGR视频流 阅读1682次,点赞1次
- Numpy - 保存和加载numpy数组、字典、列表数据 阅读1337次,点赞0次
- 资源分享 - Graphics Gems I 英文高清PDF下载 阅读2578次,点赞0次
评论
169