我们需要在json文件中记录一下总数,文件格式如下:
[{"total_count":0,"total_count1":0,"total_count2":0,"total_count3":0,"total_count4":0}]
目录
static_libs: [
"libjsoncpp",
],
#include
- 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}]
- 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();