Qt?http編程之nlohmann?json庫使用詳解
一、nlohman json庫
1、C++常用Json庫
- Jsoncpp
- boost ison
- Qt Json (不推薦使用)
- nlohman::json (推薦使用)下
Qt有json解析相關(guān)的類,但是并不推薦使用,原因很簡單,Qt中json解析的相關(guān)類只在qt中有用,為了避免以后不用qt就不會解析json了,建議是使用nlohmann/json,這個json適用于任何C++框架
2、什么是 nlohman::json
2.1下載地址
git repo: https://github.com/nlohmann/json
2.2基本介紹
nlohmann是一個C++的JSON庫,它提供了方便的方式來解析、生成和操作JSON數(shù)據(jù)。該庫由nlohmann編寫,是一個開源項目,被廣泛應(yīng)用于C++開發(fā)中。
nlohmann庫提供了簡單易用的API,可以輕松地將JSON數(shù)據(jù)解析為C++對象,或者將C++對象序列化為JSON數(shù)據(jù)。它支持各種數(shù)據(jù)類型,包括字符串、數(shù)字、布爾值、數(shù)組和對象等。我們可以使用簡潔的語法來訪問和操作JSON數(shù)據(jù),使得編寫JSON處理代碼變得更加簡單和高效。
除了基本的JSON解析和生成功能,nlohmann庫還提供了一些高級功能,如JSON合并、JSON差異比較、JSON數(shù)據(jù)查詢等。這些功能可以幫助我們更方便地處理復(fù)雜的JSON數(shù)據(jù),提高代碼的可維護性和可讀性。
二、具體解析示例
1、引入頭文件
nlohman:json只需要一個頭文件json.hpp,不需要編譯成lib,直接放到項目中即可使用
#include <iostream> #include "json.hpp" #include <string> //取別名方便下面使用 using json_t = nlohmann::json; using namespace std;
2、具體思路
2.1自定義json數(shù)據(jù)
//如果是沒有多層嵌套的看一下怎么拿到 json_t jRawdata; //定義一個json對象 jRawdata["username"] = "jackli"; jRawdata["pwd"] = "123456";
2.2dump方法序列化
這個適用于json格式只有一層的
// 序列化,把json數(shù)據(jù)轉(zhuǎn)成string string _rawdata = jRawdata.dump(); cout << _rawdata << endl;
2.3parse方法反序列化
// 反序列化,把string數(shù)據(jù)轉(zhuǎn)成json string json_str = R"( { "date": "20220701", "level": 1, "msg": "register account", "status": "success", "username": "jackli" } )"; try { //關(guān)鍵的parse方法 json_t j2 = json_t::parse(json_str); string date = j2["date"]; cout << date << endl; }
2.4異常的catch參數(shù)
catch (std::exception& e){}
3、整體代碼
int main() { //如果是沒有多層嵌套的看一下怎么拿到 json_t jRawdata; //定義一個json對象 jRawdata["username"] = "jackli"; jRawdata["pwd"] = "123456"; // 序列化,把json數(shù)據(jù)轉(zhuǎn)成string string _rawdata = jRawdata.dump(); cout << _rawdata << endl; // 反序列化,把string數(shù)據(jù)轉(zhuǎn)成json string json_str = R"( { "date": "20220701", "level": 1, "msg": "register account", "status": "success", "username": "jackli" } )"; try { //關(guān)鍵的parse方法 json_t j2 = json_t::parse(json_str); string date = j2["date"]; cout << date << endl; } catch (std::exception& e) { cout << "json parse failed" << endl; return -1; } cout << "解析json數(shù)組" << endl; if (parse_array() != 0) { cout << "parse array failed" << endl; } return 0; }
三、多層嵌套json數(shù)據(jù)獲取
1、整體代碼
int parse_array() { string jsonstr = R"( { "id":"2022", "name":{"EnglishName":"JackMa"}, "title":[ { "company":"taobao", "position" : "AAA", "salary" : "10000" }, { "company":"zhifubao", "position" : "CCC", "salary" : "890898" }, { "company":"pingtouge", "position" : "KKK", "salary" : "76843" } ] } )"; //都放在try-catch語句里 try { //先用一個parse方法得到j(luò)son格式數(shù)據(jù) json_t jsonObj = json_t::parse(jsonstr); //嵌套的就層層獲取 //"name":{"EnglishName":"JackMa"}, json_t nameJson = jsonObj["name"]; string engName = nameJson["EnglishName"]; cout << "engName = " << engName << endl; //多層嵌套就用for循環(huán) json_t titleJson = jsonObj["title"]; int size = titleJson.size(); cout << "size = " << size << endl; //for (int i = 0; i < size; i++) //{ // /* // { // "company":"taobao", // "position" : "AAA", // "salary" : "10000" // }, // */ // cout << titleJson[i]["company"] << endl; // cout << titleJson[i]["position"] << endl; // cout << titleJson[i]["salary"] << endl; //} //智能索引 for (auto jsonItem : titleJson) { cout << jsonItem["company"] << endl; cout << jsonItem["position"] << endl; cout << jsonItem["salary"] << endl; } } catch (const std::exception&) { return -1; } return 0; }
2、for循環(huán)兩種形式
for (int i = 0; i < size; i++) { /* { "company":"taobao", "position" : "AAA", "salary" : "10000" }, */ cout << titleJson[i]["company"] << endl; cout << titleJson[i]["position"] << endl; cout << titleJson[i]["salary"] << endl; } //智能索引 //jsonItem 是一個自動推導(dǎo)的變量,會依次表示 titleJson 中的每個 JSON 對象。 for (auto jsonItem : titleJson) { cout << jsonItem["company"] << endl; cout << jsonItem["position"] << endl; cout << jsonItem["salary"] << endl; }
以上就是Qt里nlohmann json庫使用的簡單介紹。
到此這篇關(guān)于Qt http編程之nlohmann json庫使用詳解的文章就介紹到這了,更多相關(guān)Qt nlohmann json內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言判斷數(shù)是否為素數(shù)與素數(shù)輸出
大家好,本篇文章主要講的是C語言判斷數(shù)是否為素數(shù)與素數(shù)輸出,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Qt中正則表達式的常見用法(QRegularExpression類)
正則表達式即一個文本匹配字符串的一種模式,Qt中使用QRegExp類進行模式匹配,下面這篇文章主要給大家介紹了關(guān)于Qt中正則表達式的常見用法,文中介紹的是QRegularExpression類的相關(guān)資料,需要的朋友可以參考下2024-05-05Microsoft Visual C++ 6.0開發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細介紹了Microsoft Visual C++ 6.0開發(fā)環(huán)境搭建教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04