C++利用jsoncpp庫(kù)實(shí)現(xiàn)寫(xiě)入和讀取json文件
一、jsoncpp庫(kù)
我們都知道由于Json語(yǔ)法是 JavaScript 對(duì)象表示語(yǔ)法的子集。所以在Java,JavaScript等語(yǔ)言中使用起來(lái)是十分愉快的。在C++中我們使用跨平臺(tái)的開(kāi)源庫(kù)JsonCpp也能愉快的玩耍Json。
JsonCpp 是一個(gè)C++庫(kù),允許操作 JSON 值,包括序列化和反序列化到字符串和從字符串反序列化。它還可以在非序列化/序列化步驟中保留現(xiàn)有注釋,使其成為存儲(chǔ)用戶輸入文件的便捷格式。
jsoncpp常用類
1.Json::Value
Json::Value:可以表示所有支持的類型,如:int , double ,string , object, array等。其包含節(jié)點(diǎn)的類型判斷(isNull,isBool,isInt,isArray,isMember,isValidIndex等),類型獲取(type),類型轉(zhuǎn)換(asInt,asString等),節(jié)點(diǎn)獲取(get,[]),節(jié)點(diǎn)比較(重載<,<=,>,>=,==,!=),節(jié)點(diǎn)操作(compare,swap,removeMember,removeindex,append等)等函數(shù)。
2.Json::Reader
Json::Reader:將文件流或字符串創(chuàng)解析到Json::Value中,主要使用parse函數(shù)。Json::Reader的構(gòu)造函數(shù)還允許用戶使用特性Features來(lái)自定義Json的嚴(yán)格等級(jí)。
3.Json::Writer
Json::Writer:與JsonReader相反,將Json::Value轉(zhuǎn)換成字符串流等,Writer類是一個(gè)純虛類,并不能直接使用。在此我們使用 Json::Writer 的子類:Json::FastWriter(將數(shù)據(jù)寫(xiě)入一行,沒(méi)有格式),Json::StyledWriter(按json格式化輸出,易于閱讀)。
Json::Reader可以通過(guò)對(duì)Json源目標(biāo)進(jìn)行解析,得到一個(gè)解析好了的Json::Value,通常字符串或者文件輸入流可以作為源目標(biāo)。
二、json文件
{
"Address" : "北京",
"Color" : [ 0.80000000000000004, 1.0, 0.5 ],
"Date" : 1998,
"Info" :
{
"Class" : "三年級(jí)",
"Part" : "西城區(qū)",
"School" : "北京一中"
},
"Students" :
[
{
"Id" : 1,
"ontime" : true,
"sex" : "男",
"time" : "2021-01-16"
},
{
"Id" : 2,
"ontime" : true,
"sex" : "男",
"time" : "2021-01-16"
}
],
"baseType" : "學(xué)校"
}
三、讀寫(xiě)json文件
// jsoncppTest.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
#include "json/json.h"
// 寫(xiě)入json文件
void CreateJsonFile()
{
std::string strFilePath = "test.json";
Json::Value root;
root["Address"] = "北京";
root["baseType"] = "學(xué)校";
root["Date"] = 1998;
root["Info"]["School"] = "北京一中";
root["Info"]["Part"] = "西城區(qū)";
root["Info"]["Class"] = "三年級(jí)";
root["Color"].append(0.8);
root["Color"].append(1.0);
root["Color"].append(0.5);
for (int i = 0; i < 2; i++)
{
root["Students"][i]["Id"] = i+1;
root["Students"][i]["sex"] = "男";
root["Students"][i]["ontime"] = true;
root["Students"][i]["time"] = "2021-01-16";
}
Json::StyledStreamWriter streamWriter;
ofstream outFile(strFilePath);
streamWriter.write(outFile, root);
outFile.close();
std::cout << "json文件生成成功!" << endl;
}
// 讀取json文件
void ReadJsonFile()
{
std::string strFilePath = "test.json";
Json::Reader json_reader;
Json::Value rootValue;
ifstream infile(strFilePath.c_str(), ios::binary);
if (!infile.is_open())
{
cout << "Open config json file failed!" << endl;
return;
}
if (json_reader.parse(infile, rootValue))
{
string sAddress = rootValue["Address"].asString();
cout << "Address = " << sAddress << endl;
int nDate = rootValue["Date"].asInt();
cout << "Date = " << nDate << endl;
cout << endl;
string sSchool = rootValue["Info"]["School"].asString();
cout << "School = " << sSchool << endl;
cout << endl;
Json::Value colorResult = rootValue["Color"];
if (colorResult.isArray())
{
for (unsigned int i = 0; i < colorResult.size(); i++)
{
double dColor = colorResult[i].asDouble();
cout << "Color = " << dColor << endl;
}
}
cout << endl;
// 讀取值為Array的類型
Json::Value studentResult = rootValue["Students"];
if (studentResult.isArray())
{
for (unsigned int i = 0; i < studentResult.size(); i++)
{
int nId = studentResult[i]["Id"].asInt();
cout << "Id = " << nId << endl;
string sTime = studentResult[i]["time"].asString();
cout << "Time = " << sTime << endl;
}
}
}
else
{
cout << "Can not parse Json file!";
}
infile.close();
}
int main()
{
//CreateJsonFile();
ReadJsonFile();
return 0;
}
到此這篇關(guān)于C++利用jsoncpp庫(kù)實(shí)現(xiàn)寫(xiě)入和讀取json文件的文章就介紹到這了,更多相關(guān)C++ jsoncpp寫(xiě)入讀取json文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
cocos2dx-3.10 C++實(shí)現(xiàn)滾動(dòng)數(shù)字
這篇文章主要為大家詳細(xì)介紹了cocos2dx-3.10 C++實(shí)現(xiàn)滾動(dòng)數(shù)字效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
C++之實(shí)現(xiàn)快速清空vector以及釋放vector內(nèi)存
這篇文章主要介紹了C++之實(shí)現(xiàn)快速清空vector以及釋放vector內(nèi)存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作
這篇文章主要介紹了C++處理輸入字符串并轉(zhuǎn)為數(shù)組的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Qt中QHostInfo::lookupHost()函數(shù)的方法示例
QHostInfo::lookupHost()是Qt網(wǎng)絡(luò)模塊提供的異步主機(jī)信息查詢接口,用于根據(jù)主機(jī)名或IP字符串查詢對(duì)應(yīng)的主機(jī)信息,下面就來(lái)詳細(xì)的介紹一下該函數(shù),感興趣的可以了解一下2025-12-12
C語(yǔ)言實(shí)現(xiàn)父進(jìn)程主動(dòng)終止子進(jìn)程的方法總結(jié)
一般的情況,子進(jìn)程自己運(yùn)行完后,執(zhí)行exit 或者return 后,父進(jìn)程wait. waitpid收回子進(jìn)程,但子進(jìn)程是一個(gè)循環(huán)等待狀態(tài)不主動(dòng)退出,父進(jìn)程可以采用文中介紹的幾種方法,需要的朋友可以參考下2023-10-10

