eis/eqpm/jsonReader.hpp

28 lines
633 B
C++

#pragma once
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
namespace JsonHelper {
using json = nlohmann::json;
bool jsonReader(std::string file_path, json &sjson) {
// 1. 创建文件流并打开文件
std::ifstream file(file_path);
if (!file.is_open()) {
return false;
}
// 2. 解析JSON数据
try {
file >> sjson; // 方式1: 直接使用流操作符解析文件
// 也可以使用显式解析: jsonData = json::parse(file);
} catch (const std::exception &e) {
file.close();
return false;
}
// 3. 关闭文件
file.close();
return true;
}
} // namespace JsonHelper