Android中C++如何读写json文件

我们需要在json文件中记录一下总数,文件格式如下:
[{"total_count":0,"total_count1":0,"total_count2":0,"total_count3":0,"total_count4":0}]

目录

1. Android.bp中新增

2. 头文件添加

3. 向json文件写入数据

4. 读取json文件内容


1. Android.bp中新增

    static_libs: [
        "libjsoncpp",
    ], 

2. 头文件添加

#include <json/json.h>


3. 向json文件写入数据

    long long totalCount = 0;
    long long totalCount2 = 0;
    long long totalCount3 = 0;
    long long totalCount4 = 0;
    fstream fileStream;
    fileStream.open("/sdcard/test.json", ios::out | ios::in);
    if (!fileStream.is_open()) {
        return;
    }
    Json::Value root;
    Json::Value item;
    item["total_count"] = Json::Int64(totalCount);
    item["total_count2"] = Json::Int64(totalCount2);
    item["total_count3"] = Json::Int64(total_count3);
    item["total_count4"] = Json::Int64(total_count4);
    root.append(item);
    Json::FastWriter wb; 
    string str = wb.write(root);
    fileStream << str;
    fileStream.flush();

写完之后,/sdcard/test.json文件内容如下:
[{"total_count":0,"total_count1":0,"total_count2":0,"total_count3":0,"total_count4":0}]


4. 读取json文件内容

    fileStream.open("/sdcard/test.json", ios::out | ios::in);
    if (!fileStream.is_open()) {
        return;
    }
    isTotalStreamValid = true;
    char buffer[1024];
    fileStream.getline(buffer, sizeof(buffer));
    Json::Reader reader;
    Json::Value root;
    if (!reader.parse(buffer, root)) {
        return;
    }
    totalCount = root[0]["total_count"].asInt64();
    totalCount2 = root[0]["total_count2"].asInt64();
    totalCount3 = root[0]["total_count3"].asInt64();
    totalCount4 = root[0]["total_count4"].asInt64();


 

相关推荐

  1. AndroidC++如何json文件

    2024-05-12 10:46:06       12 阅读
  2. boost库json格式文件

    2024-05-12 10:46:06       35 阅读
  3. C++ 使用nlohmann/json.hpp库json字符串

    2024-05-12 10:46:06       14 阅读
  4. c++的文件

    2024-05-12 10:46:06       36 阅读
  5. C++的文件

    2024-05-12 10:46:06       45 阅读
  6. C++BMP文件

    2024-05-12 10:46:06       16 阅读
  7. C语言】文件

    2024-05-12 10:46:06       24 阅读
  8. C++二进制文件

    2024-05-12 10:46:06       11 阅读
  9. [json]定义、

    2024-05-12 10:46:06       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-12 10:46:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-12 10:46:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-12 10:46:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-12 10:46:06       18 阅读

热门阅读

  1. 跟我学C++中级篇——内联补遗

    2024-05-12 10:46:06       12 阅读
  2. oracle 递归查询(结构树)

    2024-05-12 10:46:06       10 阅读
  3. Dijkstra算法

    2024-05-12 10:46:06       7 阅读
  4. 目标检测 yolov8 pth ==> onnx

    2024-05-12 10:46:06       7 阅读
  5. mongoDB

    mongoDB

    2024-05-12 10:46:06      10 阅读
  6. 麒麟系统+飞腾处理器 qt5.9.0 编译安装教程

    2024-05-12 10:46:06       7 阅读
  7. sqlite3报错:database is locked

    2024-05-12 10:46:06       11 阅读
  8. GitLab CI/CD的原理及应用详解(四)

    2024-05-12 10:46:06       8 阅读
  9. linux系统服务器中常见故障及排查方法

    2024-05-12 10:46:06       8 阅读
  10. 【docker run --name mysql8 -d -p 3306:3306】

    2024-05-12 10:46:06       10 阅读