nlohmann/json是一個用于解析JSON的開源C++庫,口碑一流,無需額外安裝其他第三方庫,還支持單個頭文件模式,使用起來非常方便直觀。
1. 編譯
從官網(wǎng)https://github.com/nlohmann/json的Release頁面下載單個json.hpp
即可直接使用,無需單獨編譯。
2. 使用示例
下面以示例的方式羅列nlohmann/json庫的基本使用方法。
2.1 生成JSON
方式1
int main()
{
using json = nlohmann::json;
json j;
j["pi"] = 3.141;
j["happy"] = true;
j["name"] = "Niels";
j["nothing"] = nullptr;
j["answer"]["everything"] = 42;
j["list"] = { 1, 0, 2 };
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// 轉(zhuǎn)成字符串
std::string strJSON = j.dump(2); // 2個空格的縮進
std::cout << strJSON;
return 0;
}
輸出如下:
{
"answer": {
"everything": 42
},
"happy": true,
"list": [
1,
0,
2
],
"name": "Niels",
"nothing": null,
"object": {
"currency": "USD",
"value": 42.99
},
"pi": 3.141
}
方式2
int main()
{
using json = nlohmann::json;
json j = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42}}},
{"list", {1, 0, 2}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}
};
// 轉(zhuǎn)成字符串
std::string strJSON = j.dump(2);
std::cout << strJSON;
return 0;
}
輸出內(nèi)容與方式1一樣。
方式3
int main()
{
using json = nlohmann::json;
json j;
j["pi"] = 3.141;
j["happy"] = true;
j["name"] = "Niels";
j["nothing"] = nullptr;
json j_answer;
j_answer["everything"] = 42;
j["answer"] = j_answer;
json j_list = json::array();
j_list.push_back(1);
j_list.push_back(0);
j_list.push_back(2);
j["list"] = j_list;
json j_object;
j_object["currency"] = "USD";
j_object["value"] = 42.99;
j["object"] = j_object;
// 轉(zhuǎn)成字符串
std::string strJSON = j.dump(2);
std::cout << strJSON;
return 0;
}
輸出內(nèi)容與方式1一樣。
2.2 解析JSON
int main()
{
using json = nlohmann::json;
std::string strJSON = u8R"(
{
"answer": {
"everything": 42
},
"happy": true,
"list": [
1,
0,
2
],
"name": "Niels",
"nothing": null,
"object": {
"currency": "USD",
"value": 42.99
},
"pi": 3.141
}
)";
auto jsonObj = json::parse(strJSON);
std::cout << jsonObj["pi"].get<float>() << std::endl; // 3.141
std::cout << jsonObj["pi"].get<double>() << std::endl; // 3.141
std::cout << std::boolalpha << jsonObj["happy"].get<bool>() << std::endl; // true
std::cout << jsonObj["name"].get<std::string>() << std::endl; // Niels
assert(jsonObj["nothing"] == nullptr);
std::cout << jsonObj["answer"]["everything"].get<int>() << std::endl; // 42
std::cout << jsonObj["list"].size() << std::endl; // 3
std::cout << jsonObj["list"][0].get<int>() << std::endl; // 1
std::cout << jsonObj["list"][1].get<int>() << std::endl; // 0
std::cout << jsonObj["list"][2].get<int>() << std::endl; // 2
std::cout << jsonObj["object"]["currency"].get<std::string>() << std::endl; // USD
std::cout << jsonObj["object"]["value"].get<float>() << std::endl; // 42.99
// 依次輸出:
// 1
// 0
// 2
for (json::iterator it = jsonObj["list"].begin(); it != jsonObj["list"].end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}
3. 異常處理
當解析和生成JSON出錯時,nlohmann/json會拋出異常,因此在解析和生成JSON時,需要進行異常捕獲。文章來源:http://www.zghlxwxcb.cn/news/detail-565568.html
int main()
{
using json = nlohmann::json;
std::string strJSON = u8R"(
{
"pi": 3.141
}
)";
try {
auto jsonObj = json::parse(strJSON);
std::cout << jsonObj["ppp"].get<float>() << std::endl;
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
4. 判斷成員是否存在
int main()
{
using json = nlohmann::json;
std::string strJSON = u8R"(
{
"pi": 3.141
}
)";
auto jsonObj = json::parse(strJSON);
std::cout << std::boolalpha << jsonObj.contains("pi") << std::endl; // true
std::cout << std::boolalpha << jsonObj.contains("ppp") << std::endl; // false
return 0;
}
歡迎訪問我的個人站點:https://jiangxueqiao.com文章來源地址http://www.zghlxwxcb.cn/news/detail-565568.html
到了這里,關(guān)于開源庫nlohmann json使用備忘的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!