mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-28 03:09:24 +08:00
完成系统管理web端功能,实现系统管理服务端接口,实现登录功能
This commit is contained in:
67
src/common/JsonN.h
Normal file
67
src/common/JsonN.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
|
||||
using NJson = nlohmann::json;
|
||||
|
||||
/// =============================================================================================
|
||||
/// 使用说明:
|
||||
|
||||
/// 创建 -------------
|
||||
// json j;
|
||||
// j["name"] = "Alice";
|
||||
// j["age"] = 25;
|
||||
// j["address"] = {{"city", "Beijing"}, {"country", "China"}};
|
||||
|
||||
/// 字符串解析 -------------
|
||||
// json::parse(R"({"name": "Alice", "age": 25})");
|
||||
// 说明:解析失败会抛出异常
|
||||
// try {
|
||||
// auto j = json::parse("invalid json");
|
||||
// }
|
||||
// catch (json::parse_error& e) {
|
||||
// std::cerr << "JSON 解析错误: " << e.what() << std::endl;
|
||||
// }
|
||||
|
||||
/// 文件解析 -------------
|
||||
// std::ifstream file("data.json");
|
||||
// json j;
|
||||
// file >> j;
|
||||
|
||||
/// 访问 JSON 数据 -------------
|
||||
// std::string name = j["name"]; // 直接访问
|
||||
// int age = j.at("age"); // 使用 at() 检查 key 是否存在
|
||||
// auto address = j["address"]; // 获取数组
|
||||
|
||||
/// 序列化为字符串 -------------
|
||||
// std::string json_str = j.dump(4); // 4 表示缩进,美化输出
|
||||
|
||||
static bool NJsonLoad(std::string jsonfile, NJson& json)
|
||||
{
|
||||
std::ifstream ifs(jsonfile);
|
||||
if (ifs.is_open())
|
||||
{
|
||||
ifs >> json;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool NJsonParse(std::string jsonstr, NJson& json)
|
||||
{
|
||||
try
|
||||
{
|
||||
json = NJson::parse(jsonstr);
|
||||
}
|
||||
catch (nlohmann::json::parse_error& e)
|
||||
{
|
||||
//std::cerr << "JSON 解析错误: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool NJsonLHas(NJson& json, std::string key)
|
||||
{
|
||||
return json.contains("database");
|
||||
}
|
||||
Reference in New Issue
Block a user