前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【C++】开源:nlohmann/json数据解析库配置使用

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

作者头像
DevFrank
发布2024-07-24 16:08:37
50
发布2024-07-24 16:08:37
举报
文章被收录于专栏:C++开发学习交流

😏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 对象的创建和序列化示例:

代码语言:javascript
复制
#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 反序列化示例:

代码语言:javascript
复制
#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++ 标准数据类型:

代码语言:javascript
复制
#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示例:

代码语言:javascript
复制
#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 文件示例:

代码语言:javascript
复制
#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 文件示例:

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 😏1. 项目介绍
  • 😊2. 环境配置
  • 😆3. 使用说明
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档