#include "health_service.h"
#include <iostream>
-#include <fstream>
#include "wearable_log.h"
#include "cJSON.h"
#include <sys/stat.h>
#include <ctime>
#include "kv_store.h"
#include "custom_utils.h"
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <cstdint>
namespace OHOS
{
return false;
}
- std::string jsonDataString((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
+ // 读取文件内容到字符串
+ std::stringstream buffer;
+ buffer << inFile.rdbuf();
+ std::string jsonDataString = buffer.str();
const char *jsonData = jsonDataString.c_str();
printf("jsonData:%s\r\n", jsonData);
if (jsonDataString.empty())
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " jsonDataString is error");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonDataString is empty");
+ inFile.close();
+ return false;
+ }
+
+ cJSON *root = cJSON_Parse(jsonData);
+ if (root == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to parse JSON data");
inFile.close();
return false;
}
if (type == HEALTH_TYPE_HEART)
{
- heart_root = cJSON_Parse(jsonData);
+ heart_root = root;
}
- if (type == HEALTH_TYPE_OXYGEN)
+ else if (type == HEALTH_TYPE_OXYGEN)
{
- oxygen_root = cJSON_Parse(jsonData);
+ oxygen_root = root;
}
inFile.close();