欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++利用jsoncpp庫實(shí)現(xiàn)寫入和讀取json文件

 更新時(shí)間:2023年04月20日 08:35:40   作者:歐特克_Glodon  
JsonCpp 是一個(gè)C++庫,允許操作 JSON 值,包括序列化和反序列化到字符串和從字符串反序列化。本文主要介紹了如何利用jsoncpp庫實(shí)現(xiàn)寫入和讀取json文件,感興趣的可以了解一下

一、jsoncpp庫

我們都知道由于Json語法是 JavaScript 對象表示語法的子集。所以在Java,JavaScript等語言中使用起來是十分愉快的。在C++中我們使用跨平臺的開源庫JsonCpp也能愉快的玩耍Json。

JsonCpp 是一個(gè)C++庫,允許操作 JSON 值,包括序列化和反序列化到字符串和從字符串反序列化。它還可以在非序列化/序列化步驟中保留現(xiàn)有注釋,使其成為存儲用戶輸入文件的便捷格式。

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來自定義Json的嚴(yán)格等級。

3.Json::Writer

Json::Writer:與JsonReader相反,將Json::Value轉(zhuǎn)換成字符串流等,Writer類是一個(gè)純虛類,并不能直接使用。在此我們使用 Json::Writer 的子類:Json::FastWriter(將數(shù)據(jù)寫入一行,沒有格式),Json::StyledWriter(按json格式化輸出,易于閱讀)。

Json::Reader可以通過對Json源目標(biāo)進(jìn)行解析,得到一個(gè)解析好了的Json::Value,通常字符串或者文件輸入流可以作為源目標(biāo)。

二、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" : "學(xué)校"
}

三、讀寫json文件

// jsoncppTest.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。
//
#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"] = "學(xué)校";
	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;
}

到此這篇關(guān)于C++利用jsoncpp庫實(shí)現(xiàn)寫入和讀取json文件的文章就介紹到這了,更多相關(guān)C++ jsoncpp寫入讀取json文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論