在C++项目中处理JSON格式的数据是很多开发场景下的必备需求,比如前后端接口通信、配置文件读取、第三方API数据解析等,掌握JSON解析的实现方法能大幅提升开发效率。

常用C++ JSON解析库介绍
目前C++生态中有多个成熟的JSON解析库,其中使用最广泛的是nlohmann_json和rapidjson,两者各有特点:
- nlohmann_json:接口设计非常友好,支持现代C++语法,使用起来和原生数据类型操作逻辑接近,学习成本低,适合快速开发场景。
- rapidjson:由腾讯开发,性能表现优异,内存占用低,支持SAX和DOM两种解析模式,适合对性能要求较高的项目。
使用nlohmann_json实现JSON解析
环境配置
可以通过包管理器安装,也可以直接下载头文件引入项目。如果是使用vcpkg的话,执行以下命令即可安装:
vcpkg install nlohmann-json
基础解析示例
下面是解析简单JSON字符串的示例代码:
#include <iostream>
#include <nlohmann/json.hpp>
// 引入json别名
using json = nlohmann::json;
int main() {
// 待解析的JSON字符串
std::string json_str = R"({"name": "张三", "age": 25, "is_student": true})";
try {
// 解析JSON字符串
json j = json::parse(json_str);
// 获取字段值,支持类型自动转换
std::string name = j["name"];
int age = j["age"];
bool is_student = j["is_student"];
std::cout << "姓名:" << name << std::endl;
std::cout << "年龄:" << age << std::endl;
std::cout << "是否是学生:" << (is_student ? "是" : "否") << std::endl;
} catch (const json::parse_error& e) {
// 捕获解析错误
std::cerr << "JSON解析失败:" << e.what() << std::endl;
}
return 0;
}
解析复杂JSON结构
如果遇到嵌套的JSON或者数组结构,也可以很方便地处理:
#include <iostream>
#include <nlohmann/json.hpp>
#include <vector>
using json = nlohmann::json;
int main() {
// 包含数组和嵌套对象的JSON
std::string json_str = R"({
"class_name": "高三一班",
"students": [
{"name": "李四", "score": 92},
{"name": "王五", "score": 88}
]
})";
json j = json::parse(json_str);
std::string class_name = j["class_name"];
std::cout << "班级名称:" << class_name << std::endl;
// 遍历学生数组
std::cout << "学生列表:" << std::endl;
for (auto& student : j["students"]) {
std::string name = student["name"];
int score = student["score"];
std::cout << " 姓名:" << name << ",分数:" << score << std::endl;
}
return 0;
}
使用rapidjson实现JSON解析
环境配置
rapidjson是只有头文件的库,直接从官方仓库下载源码,将include目录添加到项目的头文件搜索路径即可使用。
基础解析示例
下面是使用rapidjson解析相同JSON字符串的代码:
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
using namespace rapidjson;
int main() {
// 待解析的JSON字符串
const char* json_str = "{"name": "张三", "age": 25, "is_student": true}";
// 创建文档对象并解析
Document doc;
doc.Parse(json_str);
// 检查解析是否成功
if (doc.HasParseError()) {
std::cerr << "JSON解析失败" << std::endl;
return -1;
}
// 获取字段值,需要显式指定类型
std::string name = doc["name"].GetString();
int age = doc["age"].GetInt();
bool is_student = doc["is_student"].GetBool();
std::cout << "姓名:" << name << std::endl;
std::cout << "年龄:" << age << std::endl;
std::cout << "是否是学生:" << (is_student ? "是" : "否") << std::endl;
return 0;
}
处理数组和嵌套对象
rapidjson处理复杂结构的示例如下:
#include <iostream>
#include <rapidjson/document.h>
using namespace rapidjson;
int main() {
const char* json_str = "{"class_name": "高三一班", "students": [{"name": "李四", "score": 92}, {"name": "王五", "score": 88}]}";
Document doc;
doc.Parse(json_str);
std::string class_name = doc["class_name"].GetString();
std::cout << "班级名称:" << class_name << std::endl;
// 获取学生数组
const Value& students = doc["students"];
std::cout << "学生列表:" << std::endl;
for (SizeType i = 0; i < students.Size(); i++) {
std::string name = students[i]["name"].GetString();
int score = students[i]["score"].GetInt();
std::cout << " 姓名:" << name << ",分数:" << score << std::endl;
}
return 0;
}
两种库的选择建议
| 对比维度 | nlohmann_json | rapidjson |
|---|---|---|
| 易用性 | 高,接口接近原生语法 | 中等,需要熟悉库的类型接口 |
| 性能 | 中等 | 高,解析速度快内存占用低 |
| 依赖 | 仅需要C++11及以上标准 | 仅需要C++11及以上标准 |
| 适用场景 | 快速开发、业务逻辑复杂的项目 | 性能敏感、底层服务类项目 |
开发者可以根据项目的具体需求选择合适的JSON解析库,两种库都能很好地满足C++中JSON解析的基本需求,掌握其中一种即可应对大部分开发场景。
C++JSON解析nlohmann_jsonrapidjson修改时间:2026-06-24 00:21:38