当前位置: 首页 > news >正文

C++ 使用 nlohmann::json存储json文件

C++ 使用 nlohmann::json存储json文件

  • nlohmann::json 概述
  • JSON 存储的示例
  • 以追加的方式存储json文件

nlohmann::json 概述

nlohmann::json 是 C++ 中一个流行的 JSON 库,由 Niels Lohmann 开发。它提供了一个简单而强大的 API,用于解析、构建、操作和序列化 JSON 数据。

nlohmann::json 是一个模板类,可以用来表示 JSON 数据。它可以表示 JSON 对象、数组、数值、字符串、布尔值和空值等各种类型。nlohmann::json 支持方便的成员函数和操作符重载,使得对 JSON 数据的访问和修改非常直观和简便。

JSON 存储的示例

#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"using json = nlohmann::json;int main() {// 创建一个复杂的嵌套 JSON 对象json data = {{"name", "John"},{"age", 30},{"is_student", false},{"grades", {85, 92, 78, 90}},{"address", {{"street", "123 Main St"},{"city", "New York"},{"country", "USA"}}},{"friends", {{{"name", "Alice"}, {"age", 28}},{{"name", "Bob"}, {"age", 32}},{{"name", "Claire"}, {"age", 27}}}}};// 将 JSON 对象写入文件std::ofstream file("data.json");if (file.is_open()) {file << std::setw(4) << data << std::endl;file.close();std::cout << "JSON data has been written to file." << std::endl;} else {std::cerr << "Failed to open file for writing." << std::endl;}return 0;
}

以追加的方式存储json文件

在打开文件时使用 std::ofstream 的 std::ios_base::app 模式来追加写入数据。

std::ofstream json_out_file_("data.json", std::ios_base::app);

以下是 nlohmann::json 常见的用法和功能:

  1. 解析和构建 JSON 数据:

    nlohmann::json json_data = nlohmann::json::parse(json_string);  // 解析 JSON 字符串为 JSON 对象
    nlohmann::json json_data = {{ "key", "value" }, { "array", {1, 2, 3} }};  // 构建 JSON 对象// 在 JSON 对象中添加新的字段或修改现有字段
    json_data["new_key"] = "new_value";
    json_data["existing_key"] = 123;// 创建 JSON 数组
    nlohmann::json json_array = {1, 2, 3, 4, 5};
    
  2. 访问和操作 JSON 数据:

    std::string value = json_data["key"];  // 获取 JSON 对象中指定字段的值
    int size = json_array.size();  // 获取 JSON 数组的长度
    bool is_object = json_data.is_object();  // 检查 JSON 数据是否为对象// 遍历 JSON 对象或数组的元素
    for (const auto& element : json_data) {std::string key = element.first;nlohmann::json value = element.second;// 处理元素
    }// 修改 JSON 数组的元素
    json_array[2] = 10;
    
  3. 序列化和反序列化 JSON 数据:

    std::string serialized_json = json_data.dump();  // 将 JSON 对象序列化为字符串// 从文件中读取 JSON 数据并解析
    std::ifstream input_file("data.json");
    nlohmann::json json_data;
    input_file >> json_data;
    

nlohmann::json 提供了一种便捷和高效的方式来处理 JSON 数据,使得在 C++ 程序中解析、生成和操作 JSON 变得更加简单。它非常适合于处理各种 JSON 数据,包括配置文件、API 响应和数据交换等。

http://www.lryc.cn/news/312346.html

相关文章:

  • 何为OOM(Out of Memory)?
  • SpringBoot+Mybatis-plus+shardingsphere实现分库分表
  • FPGA DDR3简介及时序
  • java网络编程 02 socket
  • 【Web安全】SQL各类注入与绕过
  • C++ 设计模式
  • 安卓使用ExoPlayer出现膨胀类异常
  • C++之析构函数
  • 108. 将有序数组转换为二叉搜索树【简单】
  • vue3中watch和watchEffect的区别!!!
  • 【JavaEE初阶 -- 计算机核心工作机制】
  • springcloud:3.6测试信号量隔离
  • AI化未来:智能科技的新纪元
  • Unity 整体界面淡入淡出效果
  • 反序列化逃逸 [安洵杯 2019]easy_serialize_php1
  • JavaScript中的包装类型详解
  • 如何向各大媒体网站投稿 海外媒体发稿平台有哪些
  • 基于SpringBoot的论坛系统(附项目源码+论文)
  • 堆以及堆的实现
  • 使用RabbitMQ实现延时消息自动取消的简单案例
  • Docker部署(ruoyi案例接上篇Docker之部署前后端分离项目)实施必会!!!!
  • 电脑中已经有多个模组压缩文件,如何通过小火星露谷管理器批量安装
  • [Linux]如何理解kernel、shell、bash
  • C++:Vector的使用
  • Redis之事务(详细解析)
  • Java项目:39 springboot007大学生租房平台的设计与实现
  • 安卓内存信息查看
  • Positional Encoding 位置编码
  • MySql、Navicat 软件安装 + Navicat简单操作(建数据库,表)
  • 逆向案例五、爬取b站评论,表单MD5加密