?一、安裝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"
}