C++ qt 使用jsoncpp json 讀寫操作
JsonCpp的使用
項(xiàng)目需要c++下使用json,我選擇了JsonCpp,官網(wǎng)是:https://github.com/open-source-parsers/jsoncpp。
解壓后使用python編譯出兩個(gè)h文件和一個(gè)cpp文件:
(電腦需要安裝python自己百度安裝,這里就不說(shuō)了)
安裝python后,打開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)有格式寫入一個(gè)是有格式寫入,如下:

Demo工程下載地址:點(diǎn)擊此處下載
到此這篇關(guān)于C++ qt 使用jsoncpp json 讀寫的文章就介紹到這了,更多相關(guān)C++ qt 使用jsoncpp json內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作(一)
本篇文章是對(duì)用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++的get()函數(shù)與getline()函數(shù)使用詳解
這篇文章主要介紹了C++的get()函數(shù)與getline()函數(shù)使用詳解,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
深入了解C語(yǔ)言中的動(dòng)態(tài)內(nèi)存分配
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言中的動(dòng)態(tài)內(nèi)存分配,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語(yǔ)言有一定的幫助,需要的可以參考一下2022-06-06
詳解C語(yǔ)言中的rename()函數(shù)和remove()函數(shù)的使用方法
這篇文章主要介紹了詳解C語(yǔ)言中的rename()函數(shù)和remove()函數(shù)的使用方法,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09

