您的位置:首页 > 文旅 > 美景 > 徐州信息网查询中心_深圳市住建局造价站_拼多多seo 优化软件_百度收录入口在哪里查询

徐州信息网查询中心_深圳市住建局造价站_拼多多seo 优化软件_百度收录入口在哪里查询

2024/10/6 11:24:37 来源:https://blog.csdn.net/qq_43572400/article/details/142711362  浏览:    关键词:徐州信息网查询中心_深圳市住建局造价站_拼多多seo 优化软件_百度收录入口在哪里查询
徐州信息网查询中心_深圳市住建局造价站_拼多多seo 优化软件_百度收录入口在哪里查询

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"];
}
  1. 解析 JSON 字符串
    假设你有一个 JSON 格式的字符串,想要将其解析为 C++ 对象
#include <iostream>
#include <nlohmann/json.hpp>int 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::vector<std::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;
}
  1. 生成 JSON 对象
    你可以使用 nlohmann::json 轻松地创建一个 JSON 对象,并将其转换为字符串。
#include <iostream>
#include <nlohmann/json.hpp>int 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;
}
  1. 修改 JSON 数据
    你可以直接对 JSON 对象中的数据进行修改:
#include <iostream>
#include <nlohmann/json.hpp>int 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;
}
  1. 将 JSON 写入文件
    你可以将生成的 JSON 对象保存到文件中,或者从文件中读取 JSON 数据。
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>int 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;
}
  1. 遍历 JSON 对象
    你可以迭代遍历 nlohmann::json 对象来访问其键值对。
#include <iostream>
#include <nlohmann/json.hpp>int 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.hpp>struct Person {std::string name;int age;// 使用 NLOHMANN_DEFINE_TYPE_INTRUSIVE 宏NLOHMANN_DEFINE_TYPE_INTRUSIVE(Person, name, age)
};

序列化和反序列化
使用该宏后,您可以轻松地将 Person 对象转换为 JSON,或从 JSON 创建 Person 对象:

#include <iostream>int 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.get<Person>();std::cout << new_person.name << ", " << new_person.age << std::endl;  // 输出: Alice, 30return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com