免费试用网站空间,职业生涯规划大赛ppt,重庆市建设工程信息网中标公告,天眼查官方网站nlohmann/json安装与应用 1. nlohmann/json简介1.1 nlohmann/json下载 2. C示例2.1 应用示例2.2 封装应用2.2.1 NLOHMANN_DEFINE_TYPE_INTRUSIVE 1. nlohmann/json简介
优点#xff1a;
易用性#xff1a;该库的接口设计非常直观#xff0c;与 C STL 风格非常接近。单头文… nlohmann/json安装与应用 1. nlohmann/json简介1.1 nlohmann/json下载 2. C示例2.1 应用示例2.2 封装应用2.2.1 NLOHMANN_DEFINE_TYPE_INTRUSIVE 1. nlohmann/json简介
优点
易用性该库的接口设计非常直观与 C STL 风格非常接近。单头文件它是一个单头文件库只需要将头文件引入项目即可使用。功能强大支持从 JSON 格式到 C 标准容器如 std::vector、std::map的自动转换支持序列化、反序列化操作。轻量化不依赖其他库且解析速度较快。
缺点
可能在处理非常大的 JSON 文件时性能不是最优。
1.1 nlohmann/json下载
source地址GitHubhttps://github.com/nlohmann/json
2. C示例
2.1 应用示例
#include nlohmann/json.hpp
using json nlohmann::json;int main() {// 解析 JSON 字符串std::string jsonString R({name: John, age: 30});json jsonObj json::parse(jsonString);// 访问 JSON 对象std::string name jsonObj[name];int age jsonObj[age];
}解析 JSON 字符串 假设你有一个 JSON 格式的字符串想要将其解析为 C 对象
#include iostream
#include nlohmann/json.hppint main() {// JSON 字符串std::string json_str R({name: Alice,age: 25,is_student: false,skills: [C, Python, JavaScript]});// 解析 JSON 字符串为 JSON 对象nlohmann::json json_obj nlohmann::json::parse(json_str);// 访问 JSON 数据std::string name json_obj[name];int age json_obj[age];bool is_student json_obj[is_student];std::vectorstd::string skills json_obj[skills];// 输出结果std::cout Name: name \n;std::cout Age: age \n;std::cout Is student: (is_student ? Yes : No) \n;std::cout Skills: ;for (const auto skill : skills) {std::cout skill ;}std::cout std::endl;return 0;
}生成 JSON 对象 你可以使用 nlohmann::json 轻松地创建一个 JSON 对象并将其转换为字符串。
#include iostream
#include nlohmann/json.hppint main() {// 创建 JSON 对象nlohmann::json json_obj;json_obj[name] Bob;json_obj[age] 30;json_obj[is_student] true;json_obj[skills] {Java, Kotlin, Swift};// 转换为字符串std::string json_str json_obj.dump(4); // 参数 4 表示缩进 4 空格std::cout json_str std::endl;return 0;
}修改 JSON 数据 你可以直接对 JSON 对象中的数据进行修改
#include iostream
#include nlohmann/json.hppint main() {// 创建一个初始的 JSON 对象nlohmann::json json_obj {{name, Charlie},{age, 22},{is_student, true},{skills, {C#, Go}}};// 修改值json_obj[age] 23;json_obj[skills].push_back(Rust);// 输出修改后的 JSONstd::cout json_obj.dump(4) std::endl;return 0;
}将 JSON 写入文件 你可以将生成的 JSON 对象保存到文件中或者从文件中读取 JSON 数据。
#include iostream
#include fstream
#include nlohmann/json.hppint main() {// 创建 JSON 对象nlohmann::json json_obj {{name, Dave},{age, 40},{is_student, false},{skills, {PHP, Ruby}}};// 将 JSON 写入文件std::ofstream file(data.json);file json_obj.dump(4); // 缩进 4 个空格file.close();// 从文件读取 JSONstd::ifstream input_file(data.json);nlohmann::json read_json;input_file read_json;// 输出读取的 JSONstd::cout read_json.dump(4) std::endl;return 0;
}
遍历 JSON 对象 你可以迭代遍历 nlohmann::json 对象来访问其键值对。
#include iostream
#include nlohmann/json.hppint main() {// 创建 JSON 对象nlohmann::json json_obj {{name, Eve},{age, 28},{is_student, false},{skills, {Scala, Haskell, Elixir}}};// 遍历 JSON 对象for (auto it json_obj.begin(); it ! json_obj.end(); it) {std::cout it.key() : it.value() std::endl;}return 0;
}2.2 封装应用
2.2.1 NLOHMANN_DEFINE_TYPE_INTRUSIVE
NLOHMANN_DEFINE_TYPE_INTRUSIVE 是一个宏属于 nlohmann/json 库用于简化 C 类型与 JSON 数据之间的序列化与反序列化过程。这个宏的主要作用是自动生成将类或结构体的成员与 JSON 字段进行映射的代码。
#include nlohmann/json.hppstruct Person {std::string name;int age;// 使用 NLOHMANN_DEFINE_TYPE_INTRUSIVE 宏NLOHMANN_DEFINE_TYPE_INTRUSIVE(Person, name, age)
};序列化和反序列化 使用该宏后您可以轻松地将 Person 对象转换为 JSON或从 JSON 创建 Person 对象
#include iostreamint main() {// 创建一个 Person 对象Person person{Alice, 30};// 序列化为 JSONnlohmann::json j person;std::cout j.dump() std::endl; // 输出: {name:Alice,age:30}// 从 JSON 反序列化为 Person 对象Person new_person j.getPerson();std::cout new_person.name , new_person.age std::endl; // 输出: Alice, 30return 0;
}