C++利用jsoncpp庫實現(xiàn)寫入和讀取json文件
一、jsoncpp庫
我們都知道由于Json語法是 JavaScript 對象表示語法的子集。所以在Java,JavaScript等語言中使用起來是十分愉快的。在C++中我們使用跨平臺的開源庫JsonCpp也能愉快的玩耍Json。
JsonCpp 是一個C++庫,允許操作 JSON 值,包括序列化和反序列化到字符串和從字符串反序列化。它還可以在非序列化/序列化步驟中保留現(xiàn)有注釋,使其成為存儲用戶輸入文件的便捷格式。
jsoncpp常用類
1.Json::Value
Json::Value:可以表示所有支持的類型,如:int , double ,string , object, array等。其包含節(jié)點的類型判斷(isNull,isBool,isInt,isArray,isMember,isValidIndex等),類型獲取(type),類型轉(zhuǎn)換(asInt,asString等),節(jié)點獲取(get,[]),節(jié)點比較(重載<,<=,>,>=,==,!=),節(jié)點操作(compare,swap,removeMember,removeindex,append等)等函數(shù)。
2.Json::Reader
Json::Reader:將文件流或字符串創(chuàng)解析到Json::Value中,主要使用parse函數(shù)。Json::Reader的構造函數(shù)還允許用戶使用特性Features來自定義Json的嚴格等級。
3.Json::Writer
Json::Writer:與JsonReader相反,將Json::Value轉(zhuǎn)換成字符串流等,Writer類是一個純虛類,并不能直接使用。在此我們使用 Json::Writer 的子類:Json::FastWriter(將數(shù)據(jù)寫入一行,沒有格式),Json::StyledWriter(按json格式化輸出,易于閱讀)。
Json::Reader可以通過對Json源目標進行解析,得到一個解析好了的Json::Value,通常字符串或者文件輸入流可以作為源目標。
二、json文件
{ "Address" : "北京", "Color" : [ 0.80000000000000004, 1.0, 0.5 ], "Date" : 1998, "Info" : { "Class" : "三年級", "Part" : "西城區(qū)", "School" : "北京一中" }, "Students" : [ { "Id" : 1, "ontime" : true, "sex" : "男", "time" : "2021-01-16" }, { "Id" : 2, "ontime" : true, "sex" : "男", "time" : "2021-01-16" } ], "baseType" : "學校" }
三、讀寫json文件
// jsoncppTest.cpp : 定義控制臺應用程序的入口點。 // #include "stdafx.h" #include <iostream> #include <fstream> using namespace std; #include "json/json.h" // 寫入json文件 void CreateJsonFile() { std::string strFilePath = "test.json"; Json::Value root; root["Address"] = "北京"; root["baseType"] = "學校"; root["Date"] = 1998; root["Info"]["School"] = "北京一中"; root["Info"]["Part"] = "西城區(qū)"; root["Info"]["Class"] = "三年級"; 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; }
到此這篇關于C++利用jsoncpp庫實現(xiàn)寫入和讀取json文件的文章就介紹到這了,更多相關C++ jsoncpp寫入讀取json文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++?使用?new?創(chuàng)建二維數(shù)組實例
這篇文章主要介紹了C++?使用?new?創(chuàng)建二維數(shù)組實例的相關資料,需要的朋友可以參考下2023-01-01用c語言實現(xiàn)《狼人殺》游戲發(fā)牌系統(tǒng)
大家好,本篇文章主要講的是用c語言實現(xiàn)《狼人殺》游戲發(fā)牌系統(tǒng),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01