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

苏州沧浪做网站哪家好想做app推广项目在哪找

苏州沧浪做网站哪家好,想做app推广项目在哪找,如何建设网站?,最新室内设计效果图C 使用 nlohmann::json存储json文件 nlohmann::json 概述JSON 存储的示例以追加的方式存储json文件 nlohmann::json 概述 nlohmann::json 是 C 中一个流行的 JSON 库,由 Niels Lohmann 开发。它提供了一个简单而强大的 API,用于解析、构建、操作和序列化…

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.tj-hxxt.cn/news/81298.html

相关文章:

  • 定制v下载安卓青岛网站seo公司
  • web网站建设一题库百度电脑版网页版
  • 网站订制公司html做一个简单的网页
  • 做公司网站要走哪些流程全网营销网络推广
  • 做网站要先做商标吗商业推广
  • 万网网站域名云盘搜索引擎入口
  • 河北网站建设大全建一个自己的网站
  • 济南网站建设运营网络推广专员岗位职责
  • c 做的网站怎么上传图片2022年最火的新闻摘抄
  • 微信做淘宝客 网站打不开英文seo是什么
  • 电商在线官方福建seo外包
  • 网站如何做邮箱订阅号产品营销策划方案怎么做
  • 沈阳三好街附近做网站精准引流客源的方法可靠吗
  • 招聘网站如何做免费域名怎么注册
  • 今天疫情最新公布windows优化大师官方网站
  • 门户类网站如何做策划东莞网络营销公司
  • 淘宝 网站建设指数基金怎么选
  • 怎样提高网站打开速度慢百度竞价推广点击器
  • wordpress用户注册地址台州关键词优化报价
  • 品牌名字大全深圳seo论坛
  • 做招聘网站需要做什么公司班级优化大师官网下载
  • wordpress仿站js如何导入广州网站优化软件
  • 如果启动浏览器就能直接打开一个常用的网站主页_要怎么做?企业如何开展网络营销
  • 利用angular做的网站实时热榜
  • 英德住房和城乡建设局网站广告联盟点击赚钱平台
  • wordpress群晖套件廊坊seo培训
  • 微信网站开发今日郑州头条最新新闻
  • 网站友情链接模块介绍谷歌浏览器下载安装2022
  • 江门网站制作网站优量汇广告平台
  • 定制开发网站多少钱网站打开