json解析库的使用

28 篇文章 1 订阅
3 篇文章 0 订阅

前言:

使用官方json.cpp解析库解析json文件

#include <json\json.h>
#pragma comment(lib, "lib_json.lib")
/************************/
/*json写操作*/
/************************/
void json_write_test()
{
	Json::Value json_temp;
	json_temp["name"] = Json::Value("测试名字");
	json_temp["age"] = Json::Value(17);
	json_temp["job"] = Json::Value("c++工程师");

	Json::Value root;  // 表示整个 json 对象
	root["key_string"] = Json::Value("value_string");   // 赋予字符串值:"value_string"。
	root["key_number"] = Json::Value(12345);            // 赋予数值:12345。
	root["key_boolean"] = Json::Value(false);           // 赋予bool值:false。
	root["key_double"] = Json::Value(12.345);           // 赋予 double 值:12.345。
	root["key_object"] = json_temp;                     // 赋予 json::Value 对象值。
	root["key_array"].append("array_string");           // 类型为数组,对第一个元素赋值为字符串:"array_string"。
	root["key_array"].append(1234);                     // 为数组 key_array 赋值,对第二个元素赋值为:1234。
	Json::ValueType typeone = root.type();              // 获得 root 的类型,此处为 objectValue 类型。
	Json::ValueType typetwo = root["key_number"].type();// 获得 root 中某一个字段的类型
	Json::FastWriter json_write;
	std::string str_json = json_write.write(root);      // 获得 root 的内容
	std::string num_json = json_write.write(root["key_array"]); //获得 root中某个字段 的内容

	Json::StyledWriter json_stylewrite;
	std::string str_style_json = json_stylewrite.write(root);//获取root的内容并且格式化输出
	std::string arr_style_json = json_stylewrite.write(root["key_array"]);
	int x=1;
}

/************************/
/*json读操作*/
/************************/
void json_read_test()
{
	//读json字符串
	std::string str_json;
	str_json.assign("{\"name\": \"weir\",\"id\": 10,\"job\": \"engineer\",\"pay\": 123.4}");
	Json::Value json_read;
	Json::Reader reader;
	reader.parse(str_json.c_str(), json_read);
	std::string strJob = json_read["job"].asString();

	//读json文件
	std::string str_file_path;
	str_file_path.assign("C://test.json");
	Json::Value json_read_file;
	Json::Reader reader_file;
	std::ifstream is;
	is.open(str_file_path.c_str(), std::ios::binary);
	if (reader_file.parse(is, json_read_file))
	{
		std::string code;
		if (!json_read_file["files"].isNull())//访问节点是否为空
		{
			code = json_read_file["uploadid"].asString();//取节点内容
			code = json_read_file.get("uploadid", "null").asString();

			//获取files的数组个数
			int arr_file_size = json_read_file["files"].size();
			for (int i = 0; i < arr_file_size; i++)
			{
				Json::Value val_image = json_read_file["files"][i]["images"];  
				int image_size = val_image.size();  
				for(int j = 0; j < image_size; ++j)  
				{  
					std::string type = val_image[j]["type"].asString();  
					std::string url = val_image[j]["url"].asString();  
				}  
			}
		}
	}
	//把从文件中读取出来的内容格式化为json字符串
	Json::StyledWriter json_stylewrite;
	std::string str_style_json = json_stylewrite.write(json_read_file);

	is.close();
}

// test.json文件内容
// {
// 		"id": "0001",
// 		"code": "0",
// 		"msg": "",
// 		"files":
// 	[
// 	{
// 		"code": "0",
// 			"msg": "",
// 			"filename": "1.jpg",
// 			"filesize": "1000",
// 			"width": "1024",
// 			"height": "682",
// 			"images":
// 		[
// 		{
// 			"url": "qq.com",
// 				"type": "large",
// 				"width": "720",
// 				"height": "479"
// 		},
// 		{
// 			"url": "qq.com",
// 				"type": "main",
// 				"width": "200",
// 				"height": "133"
// 			}
// 		]
// 	}
// 	]
// }
//在json中添加内容
void json_add_data_test()
{
	std::string str_file_path;
	str_file_path.assign("C://test.json");
	Json::Value json_read_file;
	Json::Reader reader_file;
	std::ifstream is;
	is.open(str_file_path.c_str(), std::ios::binary);
	reader_file.parse(is, json_read_file);

	Json::Value arrayObj;   // 构建对象   
	Json::Value new_item, new_item1;  
	new_item["date"] = "2011-12-28";  
	new_item1["time"] = "22:30:36";  
	arrayObj.append(new_item);  // 插入数组成员   
	arrayObj.append(new_item1); // 插入数组成员   
	int file_size = json_read_file["files"].size();  
	for(int i = 0; i < file_size; ++i)  
	{
		json_read_file["files"][i]["exifs"] = arrayObj;   // 插入原json中 
		json_read_file["files"][i]["name"] = "weir";
	}
	//把从文件中读取出来的内容格式化为json字符串
	Json::StyledWriter json_stylewrite;
	std::string str_style_json = json_stylewrite.write(json_read_file);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值