当前位置:   article > 正文

jsoncpp库用法:读取、写入和修改json文件_如何编写json文件

如何编写json文件

1、示例json文件

{
	"Address" : "北京",
	"Color" : [ 0.80000000000000004, 1.0, 0.5 ],
	"Date" : 1998,
	"Info" : 
	{
		"Class" : "三年级",
		"Part" : "西城区",
		"School" : "北京一中"
	},
	"Students" : 
	[
		{
			"Id" : 1,
			"ontime" : true,
			"sex" : "男",
			"time" : "2021-01-16"
		},
		{
			"Id" : 2,
			"ontime" : true,
			"sex" : "男",
			"time" : "2021-01-16"
		}
	],
	"baseType" : "学校"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2、读取json文件

// jsoncppTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

#include <iostream>
#include <fstream>
using namespace std;
#include "json/json.h"

// 读取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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

3、写入json文件

#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"] = "西城区";
	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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

3、修改json文件

// 修改json文件
void ModifyJsonFile()
{
	string sFileName = "test.json";
	Json::Reader reader;
	Json::Value root;

	std::ifstream infile;
	infile.open(sFileName );
	if (!reader.parse(infile, root))
	{
		infile.close();
		return;
	}

	// 修改地址
	root["Address"] = "广东省";
	infile.close();
	
	// 输出修改文件
	string sOutFilePath = sFileName;
	ofstream outfile(sOutFilePath);
	if (!outfile.is_open())
	{
		return;
	}
	Json::StyledWriter styled_writer;
	outfile << styled_writer.write(root);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/595958
推荐阅读
相关标签
  

闽ICP备14008679号