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

苏州沧浪做网站哪家好公司要做seo

苏州沧浪做网站哪家好,公司要做seo,网站的登陆注册页面怎么做,上海嘉定网站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.wangmingla.cn/news/11288.html

相关文章:

  • 建设网站要注意哪些嘉兴优化公司
  • 网站开发公司是干嘛的网站建站流程
  • 专门做简历的网站国际机票搜索量大涨
  • 做详情页比较好的网站微信营销的方法7种
  • 赣州网站优化沈阳seo公司
  • 郑州做网站设计产品推广软文200字
  • 高级建站网站个人博客模板
  • 没有网站可以做百度推广吗外贸企业网站推广
  • 企业建设网知名的搜索引擎优化
  • b2c商城网站建设预算百度指数官方
  • 怎么做论坛社区网站微博推广方案
  • 一起做陶瓷的网站韩国热搜榜
  • 怎么做发卡网站广州网站设计建设
  • 怎么做赌钱网站代理百度公司排名多少
  • 如何寻找seo网站建设客户个人网页怎么制作
  • 做赌博游戏网站违法广州网站营销seo费用
  • 淘宝网站是怎么做的吗seo外包上海
  • 广州哪里有做网站的内部优化
  • 电子商务网站建设实验报告心得班级优化大师客服电话
  • 哪个网站可以做服装批发windows永久禁止更新
  • 网站开发中存在的问题深圳网站维护
  • 重庆大坪网站建设网站建设的一般步骤
  • 平板电脑 做网站开发网络营销课程总结
  • 建设银行官方网站 诚聘英才百度关键词搜索怎么收费
  • 设计师经常上的网站推广关键词如何优化
  • 郑州怎样建设公司网站国家重大新闻
  • 个人专业网站备案今日郑州头条最新新闻
  • 西安网站制作流程磁力宝最佳搜索引擎入口
  • 培训app的制作北京seo优化费用
  • 旅游网页设计源代码苏州百度 seo