深入浅出(五)nlohmann/json库

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");

    // 输出修改后的 JSON
    std::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();

    // 从文件读取 JSON
    std::ifstream input_file("data.json");
    nlohmann::json read_json;
    input_file >> read_json;

    // 输出读取的 JSON
    std::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};

    // 序列化为 JSON
    nlohmann::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, 30

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小老鼠不吃猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值