尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

C++开源库使用:nlohmann/json - 指南

C++开源库使用:nlohmann/json - 指南
📅 发布时间:2026/6/20 3:49:03

C++开源库使用:nlohmann/json - 指南

1. 简介

这个库应该是最火的一个json解析的c++的开源库了吧!

可它是个模板库,我基本看不懂它啊!

不过学会怎么用就够了吧,

我用它主要目的是给我的avl写测试样例时,

可以直接从json文件进行读入测试样例。

我也似乎不是第一次用这个库了,之前也用过不过没写博客记录。

2. 构建

这个库提供了一个header-only的版本,

可以直接把single_include这个文件夹给放到头文件夹下。

不过我这里用的是cmake 的fetchcontent

# Typically you don't care so much for a third party library's tests to be
# run from your own project's code.
set(JSON_BuildTests OFF CACHE INTERNAL "")
# If you only include this third party in PRIVATE source files, you do not
# need to install it when your main project gets installed.
# set(JSON_Install OFF CACHE INTERNAL "")
# Don't use include(nlohmann_json/CMakeLists.txt) since that carries with it
# unintended consequences that will break the build. It's generally
# discouraged (although not necessarily well documented as such) to use
# include(...) for pulling in other CMake projects anyways.
add_subdirectory(nlohmann_json)
...
add_library(foo ...)
...
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)

3. 使用

这个库使用起来非常简单

如果要解析一个json文件,下面的代码就够了!

#include <fstream>#include <nlohmann/json.hpp>using json = nlohmann::json;// ...std::ifstream f("example.json");json data = json::parse(f);

你如果想直接读取内容,直接访问就好了。

如果想要转化,可以在后面跟get<T>()

std::vector<
int> arr{ data["array"].get<std::vector<
int>>()
};

我主要是要用一下序列化反序列化这个功能,

就是把一个类或者对象给转化成一个json对象,最终写入到文件或者是读到内存中来。

一般的对象其实都不用你做这一步,主要是你自定义的类需要搞。

这个库提供了一个宏来帮助你来完成这个事情

比如下面

struct avl_test_case {
avl_test_case() = default;
avl_test_case(std::vector<
int>
&&v,
std::vector< avl_cont_op >&&ops,std::vector<int>&&last_order_seqs):input{std::move(v)},op{std::move(ops)},expect_seq{std::move(last_order_seqs)}{}avl_test_case(const std::vector<int>&v,const std::vector<avl_cont_op>&ops,const std::vector<int>&expect): input{v}, op{ops},expect_seq{expect}{}bool isEqual(const avl_test_case &tcase) {return tcase.expect_seq == expect_seq && tcase.input == input && tcase.op == op;}std::vector<int> input{};std::vector< avl_cont_op > op{};std::vector<int> expect_seq{};};NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(avl_test_case, input, op, expect_seq)

在生成json对象时,属性名就是对应的成员变量名。

当然你也可以自定来定义序列化和反序列化的这个过程。

就像下面那样,需要自己去实现to_json from_json这两个方法。

enum avl_op_tp {
AVL_INSERT = 0,
AVL_DEL,
AVL_FIND,
AVL_NULL_OP
};
struct avl_cont_op {
bool operator==(const avl_cont_op &op1) const{
return tp == op1.tp && cnt == op1.cnt;
}
avl_op_tp tp{AVL_NULL_OP
};
uint32_t cnt{
};
};
namespace nlohmann {
template<
>
struct adl_serializer<avl_cont_op>{static void to_json(json& j, const avl_cont_op& cont_op) {j = json::object();j["tp"] = cont_op.tp;j["cnt"] = cont_op.cnt;}static void from_json(const json& j, avl_cont_op& cont_op) {j.at("tp").get_to(cont_op.tp);j.at("cnt").get_to(cont_op.cnt);}};}

修改json文件的话,主要是拿到对应的json& 数据,注意一定是引用不然你的修改就不生效了。在修改完json数据之后还需要再写回到文件中去。

void AVLTestManager::load_data() {
std::ifstream file(filename_);
if (!file.good()) {
// 文件不存在,创建基本结构
data_ = nlohmann::json::object();
data_["hello"] = "world";
data_["tests"] = nlohmann::json::array();
} else {
try {
data_ = nlohmann::json::parse(file);
} catch (const nlohmann::json::parse_error& e) {
throw std::runtime_error("Failed to parse JSON file: " + std::string(e.what()));
}
}
}
// 保存数据到文件
void AVLTestManager::save_data() {
std::ofstream file(filename_);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file for writing: " + filename_);
}
file << std::setw(4) << data_ << std::endl;
}

再给一段代码吧,我也只是会用了。不太理解这个模样库。

还有个问题是写入json文件时,json文件的空白固定成4个了。

所以有时候有数组的对象会占据非常多的行,暂时还没有解决这个

问题,不过能跑就行。。。哈哈哈。

class AVLTestManager
{
public:
explicit AVLTestManager(const std::string &filename):filename_{filename
}
{
load_data();
}
bool addTestCase(const std::string &suite_name, const avl_test_case& test_case);
bool removeTestSuite(const std::string &suite_name);
bool removeTestCase(const std::string &suite_name, const avl_test_case& test_case);
bool removeTestCaseByIndex(const std::string &suite_name, size_t idx);
std::vector<avl_test_suite>getAllTestSuites();private:void load_data();void save_data();nlohmann::json data_;std::string filename_;};bool AVLTestManager::addTestCase(const std::string& suiteName, const avl_test_case& testCase) {auto& testsArray = data_["tests"];bool suiteFound = false;bool caseFound = false;for (auto& suite : testsArray) {if (suite["suite_name"] == suiteName) {suiteFound = true;auto& casesArray = suite["cases"];// 检查是否已存在相同的测试用例for (const auto& existingCase : casesArray) {auto existingCaseObj = existingCase.get<avl_test_case>();if (existingCaseObj.isEqual(testCase)) {caseFound = true;break;}}if (!caseFound) {casesArray.push_back(testCase);save_data();return true;}break;}}if (!suiteFound) {// 创建新套件avl_test_suite newSuite;newSuite.suite_name = suiteName;newSuite.cases.push_back(testCase);testsArray.push_back(newSuite);save_data();return true;}return false;}

4. 参考

nlohmann/json

相关新闻

  • 实用指南:JAVA学习-预科部分(路线、博客、预备基础)
  • 【最终章】-串口收发指令处理器-Verilog语法学习EP12 - 教程
  • 10.22 CSP-S模拟37/2025多校冲刺CSP模拟赛7 改题记录

最新新闻

  • CVE-2025-55182本地复现:路径遍历漏洞原理与实战利用详解
  • 麻省理工研究人员打造 Fractal 操作系统,获苹果 M1 芯片新发现
  • React写的WebVR全景看房跳转demo,带贝壳式热点导航和视角控制
  • 2026年郑州脚手架搭建公司推荐:钢管脚手架/盘口脚手架搭建拆除、室内外装修架子搭设、脚手架租赁施工怎么选 - 海棠依旧大
  • 从PHP一句话木马到Webshell大马:攻防原理与实战防御指南
  • BepInEx IL2CPP启动失败:技术原理与完整解决方案指南

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号