zoukankan      html  css  js  c++  java
  • json文件的基本读写功能

    1 从github上下载json源代码,链接如下:https://github.com/nlohmann/json.git

    2 利用json在线解析网址测试json配置文件格式是否正确,链接如下:https://www.json.cn/

    3 将下载下来的json项目的include头文件包含在自己的项目中的包含目录(VS)

    4 在使用json的cpp文件开始加上下面两条语句(必须完成第三步,否则会编译出错)

    #include <nlohmann/json.hpp>
    using json = nlohmann::json;

    3 读取json文件代码如下:

    std::map<std::string, std::map<std::string, std::string>> ReadJsonFile(
    const std::string &filepath) {
      std::map<std::string, std::map<std::string, std::string>> json_config;
      std::ifstream file(filepath);
      if (!file.is_open()) {
        file.clear();
        return json_config;
      }
      file.seekg(0, std::ios::end);
      int file_size = file.tellg();
      file.seekg(0, std::ios::beg);
    
      char *buf = new char[file_size + 1];
      memset(buf, 0, file_size + 1);
      file.read(buf, file_size);
      std::string con(buf);
      delete[]buf;
      file.close();
    
      try {
        json json_content;
        json_content = json::parse(con);
        json_config =
          json_content.get<std::map<std::string, std::map<std::string, std::string>>>();
      } catch (...) {
        return json_config;
      }
    
      return json_config;
    }

    4 写入json文件代码如下:

    bool WriteJsonFile(const std::string& file_path,
                       std::map<std::string,  std::map<std::string, std::string>> &json_config) {
      std::ofstream file(file_path,
                         std::ios::trunc | std::ios::out | std::ios::in);
    
      if (!file.is_open()) {
        return false;
      }
    
      json js;
      for (auto p : json_config) {
        std::string objectname = p.first;
        std::map<std::string, std::string> object_config = p.second;
        json jsobject;
        for (auto k : object_config) {
          jsobject[k.first] = k.second;
        }
        js[objectname] = jsobject;
      }
    
      file << js;
      return true;
    }

    5 测试代码如下:

    void TestReadJson() {
      std::map<std::string, std::map<std::string, std::string>> json_config =
            ReadJsonFile("config.json");
      if (json_config.empty()) {
        return ;
      }
      for (auto p : json_config) {
        std::cout << p.first << std::endl;
        for (auto k : p.second) {
          std::cout << "(" << k.first << "," << k.second << ")" << std::endl;
        }
      }
    }
    
    void TestWriteJson() {
      std::map<std::string, std::map<std::string, std::string>> json_config;
      json_config["username"] = { { "username1","admin" },{ "username2","administrator" } };
      json_config["udpconfig"] = { { "ipaddress","127.0.0.1" },{ "port","1234" } };
      WriteJsonFile("config.json", json_config);
    }

    注:要使上述代码可以直接运行,还需包含下面三个头文件

    #include <iostream>
    #include <fstream>
    #include <map>
  • 相关阅读:
    How to install php 7.x on CentOS 7
    Azure新建的CentOS设置root账户的密码
    远程激活.NET REFLECTOR(不能断网)
    C# WebApi 配置复杂路由不生效的问题
    在Mac上激活Adobe产品
    WIN10更新后出现无法联网的问题
    Mac安装SSHFS挂载远程服务器上的文件夹到本地
    输入三个数值,输出其中的最大值和最小值
    登录接口,只为自己能尽快吐槽一下这段代码
    随手记
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/11875918.html
Copyright © 2011-2022 走看看