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>
  • 相关阅读:
    从dotNet到VB6之模仿构造OleDbDataAdapter与dataset结合
    编程的偷懒艺术与美感
    给您参考,现在开发数据库项目用.net 2005成熟吗?还是用.net2003比较有保证
    access数据库版权及容量问题
    中天股票数据格式
    受伤与药油的最佳搭配
    关于发布各种股票软件数据格式
    .net 与flash8传递(互传)数组的技巧
    我的C语言合集
    ZOJ 1205 Martian Addition 解题报告
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/11875918.html
Copyright © 2011-2022 走看看