【C++】开源:nlohmann/json数据解析库配置使用

😏★,°:.☆( ̄▽ ̄)/$:.°★ 😏
这篇文章主要介绍nlohmann/json数据解析库配置使用。
无专精则不能成,无涉猎则不能通。——梁启超
欢迎来到我的博客,一起学习,共同进步。
喜欢的朋友可以关注一下,下次更新不迷路🥞

😏1. 项目介绍

官网:https://json.nlohmann.me/

项目Github地址:https://github.com/nlohmann/json

nlohmann/json 是一个流行的 C++ JSON 库,以其简洁易用、功能强大而闻名。它提供了 C++ 与 JSON 之间的无缝转换,支持大多数现代 C++ 特性,使得操作 JSON 数据非常方便。

1.支持 C++11 及更高版本。
2.提供了简单直观的 API。
3.支持序列化和反序列化 JSON 数据。
4.支持多种数据类型(数字、字符串、布尔、数组、对象等)。
5.支持 STL 容器与 JSON 的互操作。

😊2. 环境配置

这个json库可以只使用头文件(header-only),头文件可以在仓库的release中下载。

在这里插入图片描述

😆3. 使用说明

JSON 对象的创建和序列化示例:

#include "json.hpp"
#include <iostream>

int main() {
    // 创建 JSON 对象
    nlohmann::json jsonObj;

    // 添加数据到 JSON 对象
    jsonObj["name"] = "John";
    jsonObj["age"] = 30;
    jsonObj["is_student"] = false;
    jsonObj["skills"] = {"C++", "Python", "JavaScript"};

    // 序列化 JSON 对象为字符串
    std::string serialized = jsonObj.dump();

    // 打印 JSON 字符串
    std::cout << "Serialized JSON: " << serialized << std::endl;

    return 0;
}

JSON 反序列化示例:

#include "json.hpp"
#include <iostream>
#include <string>

int main() {
    // JSON 字符串
    std::string jsonString = R"({"name":"John","age":30,"is_student":false,"skills":["C++","Python","JavaScript"]})";

    // 解析 JSON 字符串
    nlohmann::json jsonObj = nlohmann::json::parse(jsonString);

    // 访问 JSON 数据
    std::string name = jsonObj["name"];
    int age = jsonObj["age"];
    bool is_student = jsonObj["is_student"];
    std::vector<std::string> skills = jsonObj["skills"];

    // 打印解析的数据
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Is Student: " << is_student << std::endl;
    std::cout << "Skills: ";
    for (const auto& skill : skills) {
        std::cout << skill << " ";
    }
    std::cout << std::endl;

    return 0;
}

JSON转换为 C++ 标准数据类型:

#include "json.hpp"
#include <iostream>
#include <vector>

int main() {
    // 创建 JSON 对象
    nlohmann::json jsonObj = {
        {"pi", 3.141},
        {"happy", true},
        {"name", "Niels"},
        {"answer", {
            {"everything", 42}
        }},
        {"list", {1, 0, 2}},
        {"object", {
            {"currency", "USD"},
            {"value", 42.99}
        }}
    };

    // 转换为 C++ 标准数据类型
    double pi = jsonObj["pi"];
    bool happy = jsonObj["happy"];
    std::string name = jsonObj["name"];
    int answer = jsonObj["answer"]["everything"];
    std::vector<int> list = jsonObj["list"];

    // 打印结果
    std::cout << "pi: " << pi << std::endl;
    std::cout << "happy: " << happy << std::endl;
    std::cout << "name: " << name << std::endl;
    std::cout << "answer: " << answer << std::endl;

    std::cout << "list: ";
    for (int i : list) {
        std::cout << i << " ";
    }
    std::cout << std::endl;

    return 0;
}

C++ 标准数据类型转换为 JSON示例:

#include "json.hpp"
#include <iostream>
#include <map>

int main() {
    // 创建 C++ 标准数据类型
    std::map<std::string, int> mapData = {{"key1", 1}, {"key2", 2}, {"key3", 3}};
    std::vector<int> vectorData = {10, 20, 30};

    // 转换为 JSON
    nlohmann::json jsonMap = mapData;
    nlohmann::json jsonVector = vectorData;

    // 打印结果
    std::cout << "jsonMap: " << jsonMap.dump() << std::endl;
    std::cout << "jsonVector: " << jsonVector.dump() << std::endl;

    return 0;
}

写入 JSON 文件示例:

#include "json.hpp"
#include <iostream>
#include <fstream>

int main() {
    // 创建 JSON 对象
    nlohmann::json jsonObj = {
        {"name", "John"},
        {"age", 30},
        {"is_student", false},
        {"skills", {"C++", "Python", "JavaScript"}}
    };

    // 写入 JSON 文件
    std::ofstream outFile("data.json");
    outFile << jsonObj.dump(4); // 4 表示缩进级别
    outFile.close();

    std::cout << "JSON 文件已成功写入 data.json" << std::endl;

    return 0;
}

读取 JSON 文件示例:

#include "json.hpp"
#include <iostream>
#include <fstream>

int main() {
    // 读取 JSON 文件
    std::ifstream inFile("data.json");
    nlohmann::json jsonObj;
    inFile >> jsonObj;
    inFile.close();

    // 访问 JSON 数据
    std::string name = jsonObj["name"];
    int age = jsonObj["age"];
    bool is_student = jsonObj["is_student"];
    std::vector<std::string> skills = jsonObj["skills"];

    // 打印结果
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Is Student: " << is_student << std::endl;
    std::cout << "Skills: ";
    for (const auto& skill : skills) {
        std::cout << skill << " ";
    }
    std::cout << std::endl;

    return 0;
}

在这里插入图片描述

以上。

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DevFrank

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

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

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

打赏作者

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

抵扣说明:

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

余额充值