一、安装nlohmann json
nlohmann json:安装_nlohmann安装-CSDN博客
依次执行下面指令:
git clone https://gitee.com/cuihongxi/mov_from_github.gitcd json-developmkdir buildcd buildcmake ..makesudo make install
二、安装完成后使用
#include <nlohmann/json.hpp>using json = nlohmann::json;
例程:
// 读取JSON文件std::ifstream file("config.json");if (!file.is_open()) {std::cout << "无法打开JSON文件!" << std::endl;return 1;}// 解析JSON数据json data;try {file >> data;} catch (const json::parse_error& e) {std::cout << "JSON解析错误:" << e.what() << std::endl;return 1;}// 读取JSON中的参数std::string password = data["PASSWORD"];std::string account = data["ACCOUNT"];std::string com_serial = data["COM"];PASSWORD = password.c_str();ACCOUNT = account.c_str();comName = com_serial.c_str();
JSON
{"PASSWORD": "1234","ACCOUNT": "abc","COM": "tty0"
}