From 79dc338297deca4f857ade4ab0dc3bb16de3e323 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Fri, 28 Jun 2024 13:09:13 +0800 Subject: [PATCH] =?utf8?q?=E8=A1=A5=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../health_service/health_service.cpp | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp index e7e14402c..88e3c23e6 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp @@ -1,12 +1,15 @@ #include "health_service.h" #include -#include #include "wearable_log.h" #include "cJSON.h" #include #include #include "kv_store.h" #include "custom_utils.h" +#include +#include +#include +#include namespace OHOS { @@ -112,24 +115,35 @@ namespace OHOS return false; } - std::string jsonDataString((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + // 读取文件内容到字符串 + 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(); -- 2.17.1