C++ qt 使用jsoncpp json 讀寫(xiě)操作
JsonCpp的使用
項(xiàng)目需要c++下使用json,我選擇了JsonCpp,官網(wǎng)是:https://github.com/open-source-parsers/jsoncpp。
解壓后使用python編譯出兩個(gè)h文件和一個(gè)cpp文件:
(電腦需要安裝python自己百度安裝,這里就不說(shuō)了)
安裝python后,打開(kāi)windows下cmd窗口,進(jìn)入到j(luò)soncpp文件夾? 如圖:
執(zhí)行命令:python amalgamate.py 就會(huì)生成dist文件夾 里面有?json.h json-forwards.h jsoncpp.cpp三個(gè)文件:如下
將三個(gè)文件加入到工程即可使用,我是要qt進(jìn)行測(cè)試使用:
main.cpp如下
#include <iostream> #include <fstream> #include "dist/json/json.h" using namespace std; int main(int argc, char *argv[]) { // write Json::Value people1; people1["name"] = "Dione"; people1["sex"] = "男"; people1["age"] = 24; people1["note"] = "jsoncpp write test!"; Json::Value people2; people2["name"] = "Hulis"; people2["sex"] = "女"; people2["age"] = 22; people2["note"] = "jsoncpp write test!"; Json::Value peoples; peoples.append(people1); peoples.append(people2); Json::Value writeValue; writeValue["classname"] = "三年一班"; writeValue["peoples"] = peoples; Json::FastWriter fwriter; std::string strf = fwriter.write(writeValue); std::ofstream ofsf("example_fast_writer.json"); ofsf << strf; ofsf.close(); Json::StyledWriter swriter; std::string strs = swriter.write(writeValue); std::ofstream ofss("example_styled_writer.json"); ofss << strs; ofss.close(); // read string strValue = "{\"key1\":\"111\",\"array\":[{\"key2\":\"222\"},{\"key2\":\"333\"},{\"key2\":\"444\"}]}"; Json::Reader reader; Json::Value root; if (reader.parse(strValue, root)) { std::string out = root["key1"].asString(); qDebug()<<QString::fromStdString(out); Json::Value arrayObj = root["array"]; for (int i=0; i<arrayObj.size(); i++) { out = arrayObj[i]["key2"].asString(); qDebug()<<QString::fromStdString(out); } } std::ifstream ifs("example_fast_writer.json"); if (reader.parse(ifs, root)) { std::string out = root["classname"].asString(); qDebug()<<QString::fromStdString(out); Json::Value peoples = root["peoples"]; for (int i=0; i<peoples.size(); i++) { qDebug()<<QString::fromStdString(peoples[i]["name"].asString()); qDebug()<<QString::fromStdString(peoples[i]["sex"].asString()); qDebug()<<QString::fromStdString(peoples[i]["age"].asString()); qDebug()<<QString::fromStdString(peoples[i]["note"].asString()); } } return 0; }
會(huì)生成兩個(gè)json文件,一個(gè)是沒(méi)有格式寫(xiě)入一個(gè)是有格式寫(xiě)入,如下:
Demo工程下載地址:點(diǎn)擊此處下載
到此這篇關(guān)于C++ qt 使用jsoncpp json 讀寫(xiě)的文章就介紹到這了,更多相關(guān)C++ qt 使用jsoncpp json內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT編寫(xiě)地圖實(shí)現(xiàn)設(shè)備點(diǎn)位的示例代碼
在地圖應(yīng)用的相關(guān)項(xiàng)目中,在地圖上標(biāo)識(shí)一些設(shè)備點(diǎn),并對(duì)點(diǎn)進(jìn)行交互這個(gè)功能用的最多的,于是需要一套機(jī)制可以動(dòng)態(tài)的添加、刪除、清空、重置。本文將詳細(xì)介紹這些功能如何實(shí)現(xiàn),需要的可以參考一下2022-01-01C++ 中消息隊(duì)列函數(shù)實(shí)例詳解
這篇文章主要介紹了C++ 中消息隊(duì)列函數(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06C++編寫(xiě)生成不重復(fù)的隨機(jī)數(shù)代碼
本文給大家匯總介紹了3種c++實(shí)現(xiàn)生成不重復(fù)的隨機(jī)數(shù)的函數(shù),十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-05-05深入了解C語(yǔ)言的動(dòng)態(tài)內(nèi)存管理
所謂動(dòng)態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動(dòng)態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存,本文將用5600字帶你深入了解動(dòng)態(tài)內(nèi)存管理,感興趣的可以學(xué)習(xí)一下2022-07-07Qt5.9實(shí)現(xiàn)簡(jiǎn)單的多線(xiàn)程實(shí)例(類(lèi)QThread)
Qt開(kāi)啟多線(xiàn)程,主要用到類(lèi)QThread。用一個(gè)類(lèi)繼承QThread,然后重新改寫(xiě)虛函數(shù)run()。具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09C++實(shí)現(xiàn)簡(jiǎn)單的通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單的通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Linux?C/C++實(shí)現(xiàn)網(wǎng)絡(luò)流量分析工具
網(wǎng)絡(luò)流量分析的原理基于對(duì)數(shù)據(jù)包的捕獲、解析和統(tǒng)計(jì)分析,通過(guò)對(duì)網(wǎng)絡(luò)流量的細(xì)致觀(guān)察和分析,幫助管理員了解和優(yōu)化網(wǎng)絡(luò)的性能,本文將通過(guò)C++實(shí)現(xiàn)網(wǎng)絡(luò)流量分析工具,有需要的可以參考下2023-10-10VS2017+Qt5+Opencv3.4調(diào)用攝像頭拍照并存儲(chǔ)
本文主要介紹了VS2017+Qt5+Opencv3.4調(diào)用攝像頭拍照并存儲(chǔ),實(shí)現(xiàn)了視頻,拍照,保存這三個(gè)功能。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05