rapidjson的简单使用

Write
/*
{
"Int": 1,
"Double": 12.0000001,
"String": "This is a string",
"Object": {
"name": "qq849635649",
"age": 25
},
"IntArray": [
10,
20,
30
],
"DoubleArray": [
1,
2,
3
],
"StringArray": [
"one",
"two",
"three"
],
"MixedArray": [
"one",
50,
false,
12.005
],
"People": [
{
"name": "qq849635649",
"age": 0,
"sex": true
},
{
"name": "qq849635649",
"age": 10,
"sex": false
},
{
"name": "qq849635649",
"age": 20,
"sex": true
}
]
}
*/


#include"rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include"rapidjson/writer.h"
#include<iostream>
#include<string>
using namespace std;
void WriteJson()
{
	rapidjson::Document doc;
	doc.SetObject();
	rapidjson::Document::AllocatorType & allocator = doc.GetAllocator();
	

	//1. 整型类型
	doc.AddMember("Int", 1, allocator);


	//2. 浮点类型
	doc.AddMember("Double", 12.0000001, allocator);

	//3.字符串类型
	string str = "This is a string";
	rapidjson::Value str_value(rapidjson::kStringType);
	str_value.SetString(str.c_str(), str.size());
	if (!str_value.IsNull())
	{
		doc.AddMember("String", str_value, allocator);
	}
	/*错误的字符串写法
	 *    注:以下方式不正确,可能成功,也可能失败,因为字符串写入json要重新开辟内存,
	 * 如果使用该方式的话,当数据是字符串常量的话是没问题的,如果为变量就会显示乱码,所
	 * 以为保险起见,我们显式的分配内存(无需释放)
	 doc.AddMember("String", str_value, allocator);
	*/

	//4. 结构体类型
	rapidjson::Value object(rapidjson::kObjectType);
	object.AddMember("name", "qq849635649", allocator); //注:常量是没有问题的
	object.AddMember("age", 25, allocator);
	doc.AddMember("Object", object, allocator);
	
	//5. 数组类型
	//5.1 整型数组
	rapidjson::Value IntArray(rapidjson::kArrayType);
	IntArray.PushBack(10, allocator);
	IntArray.PushBack(20, allocator);
	IntArray.PushBack(30, allocator);
	doc.AddMember("IntArray", IntArray, allocator);

	//5.2 浮点型数组
	rapidjson::Value DoubleArray(rapidjson::kArrayType);
	DoubleArray.PushBack(1.0, allocator);
	DoubleArray.PushBack(2.0, allocator);
	DoubleArray.PushBack(3.0, allocator);
	doc.AddMember("DoubleArray", DoubleArray, allocator);
	

	//5.3 字符型数组
	rapidjson::Value StringArray(rapidjson::kArrayType);
	string strValue1 = "one";
	string strValue2 = "two";
	string strValue3 = "three";
	str_value.SetString(strValue1.c_str(), strValue1.size());
	StringArray.PushBack(str_value, allocator);
	str_value.SetString(strValue2.c_str(), strValue2.size());
	StringArray.PushBack(str_value, allocator);
	str_value.SetString(strValue3.c_str(), strValue3.size());
	StringArray.PushBack(str_value, allocator);
	doc.AddMember("StringArray", StringArray, allocator);



	//5.4 结构体数组
	rapidjson::Value ObjectArray(rapidjson::kArrayType);
	for (int i = 1; i < 4; i++)
	{
		rapidjson::Value obj(rapidjson::kObjectType);
		obj.AddMember("name", "qq849635649", allocator);//注:常量是没有问题的
		obj.AddMember("age", i * 10, allocator);
		ObjectArray.PushBack(obj, allocator);
	}
	doc.AddMember("ObjectArray", ObjectArray, allocator);

	
	rapidjson::StringBuffer strBuf;
	rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
	doc.Accept(writer);


	string data = strBuf.GetString();

	cout << data << endl;

}
改进写
/*
以下方式利用使用字符串缓冲器生成方便的写json内容,比Write()函数写起来更舒服
*/
void OptimizationWrite()
{
		rapidjson::StringBuffer strBuf;
		rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);

		writer.StartObject();

		//1. 整数类型
		writer.Key("Int");
		writer.Int(1);

		//2. 浮点类型
		writer.Key("Double");
		writer.Double(12.0000001);

		//3. 字符串类型
		writer.Key("String");
		writer.String("This is a string");

		//4. 结构体类型
		writer.Key("Object");
		writer.StartObject();
		writer.Key("name");
		writer.String("qq849635649");
		writer.Key("age");
		writer.Int(25);
		writer.EndObject();

		//5. 数组类型
		//5.1 整型数组
		writer.Key("IntArray");
		writer.StartArray();
		//顺序写入即可
		writer.Int(10);
		writer.Int(20);
		writer.Int(30);
		writer.EndArray();

		//5.2 浮点型数组
		writer.Key("DoubleArray");
		writer.StartArray();
		for (int i = 1; i < 4; i++)
		{
			writer.Double(i * 1.0);
		}
		writer.EndArray();

		//5.3 字符串数组
		writer.Key("StringArray");
		writer.StartArray();
		writer.String("one");
		writer.String("two");
		writer.String("three");
		writer.EndArray();

		//5.4 混合型数组
		//这说明了,一个json数组内容是不限制类型的
		writer.Key("MixedArray");
		writer.StartArray();
		writer.String("one");
		writer.Int(50);
		writer.Bool(false);
		writer.Double(12.005);
		writer.EndArray();

		//5.5 结构体数组
		writer.Key("People");
		writer.StartArray();
		for (int i = 0; i < 3; i++)
		{
			writer.StartObject();
			writer.Key("name");
			writer.String("qq849635649");
			writer.Key("age");
			writer.Int(i * 10);
			writer.Key("sex");
			writer.Bool((i % 2) == 0);
			writer.EndObject();
		}
		writer.EndArray();

		writer.EndObject();

		string data = strBuf.GetString();
		cout << data << endl;

}


int main()
{
	WriteJson();


	return 0;
}
Read
#include"rapidjson/document.h"
#include"rapidjson/stringbuffer.h"
#include"rapidjson/writer.h"
#include<iostream>
#include<string>

//using namespace std;
using std::cout;
using std::endl;
std::string data =
"{\"Int\":1,"
"\"Double\":12.0000001,"
"\"String\":\"This is a string\","
"\"Object\":{\"name\":\"qq849635649\",\"age\":25},"
"\"IntArray\":[10,20,30],"
"\"DoubleArray\":[1.0,2.0,3.0],"
"\"StringArray\":[\"one\",\"two\",\"three\"],"
"\"MixedArray\":[\"one\",50,false,12.005],"
"\"People\":[{\"name\":\"qq849635649\",\"age\":0,\"sex\":true},"
"{\"name\":\"qq849635649\",\"age\":10,\"sex\":false},"
"{\"name\":\"qq849635649\",\"age\":20,\"sex\":true}]}";

void parse()
{
	//创建解析对象
	rapidjson::Document doc;
	//首先进行解析,没有解析错误才能进行具体字段的解析
	if (!doc.Parse(data.data()).HasParseError())
	{
		//1. 解析整数
		if (doc.HasMember("Int") && doc["Int"].IsInt())
		{
			cout << "Int = " << doc["Int"].GetInt() << endl;
		}
		//2. 解析浮点型
		if (doc.HasMember("Double") && doc["Double"].IsDouble())
		{
			cout << "Double = " << doc["Double"].GetDouble() << endl;
		}
		//3. 解析字符串
		if (doc.HasMember("String") && doc["String"].IsString())
		{
			cout << "String = " << doc["String"].GetString() << endl;
		}
		//4. 解析结构体
		if (doc.HasMember("Object") && doc["Object"].IsObject())
		{
			const rapidjson::Value& object = doc["Object"];
			if (object.HasMember("name") && object["name"].IsString())
			{
				cout << "Object.name = " << object["name"].GetString() << endl;
			}
			if (object.HasMember("age") && object["age"].IsInt())
			{
				cout << "Object.age = " << object["age"].GetInt() << endl;
			}
		}
		//5. 解析数组类型
		//5.1 整型数组类型
		if (doc.HasMember("IntArray") && doc["IntArray"].IsArray())
		{
			//5.1.1 将字段转换成为rapidjson::Value类型
			const rapidjson::Value& array = doc["IntArray"];
			//5.1.2 获取数组长度
			size_t len = array.Size();
			//5.1.3 根据下标遍历,注意将元素转换为相应类型,即需要调用GetInt()
			for (size_t i = 0; i < len; i++)
			{
				cout << "IntArray[" << i << "] = " << array[i].GetInt() << endl;
			}
		}
		//5.2 浮点型数组类型
		if (doc.HasMember("DoubleArray") && doc["DoubleArray"].IsArray())
		{
			const rapidjson::Value& array = doc["DoubleArray"];
			size_t len = array.Size();
			for (size_t i = 0; i < len; i++)
			{
				//为防止类型不匹配,一般会添加类型校验
				if (array[i].IsDouble())
				{
					cout << "DoubleArray[" << i << "] = " << array[i].GetDouble() << endl;
				}
			}
		}
		//5.3 字符串数组类型
		if (doc.HasMember("StringArray") && doc["StringArray"].IsArray())
		{
			const rapidjson::Value& array = doc["StringArray"];
			size_t len = array.Size();
			for (size_t i = 0; i < len; i++)
			{
				//为防止类型不匹配,一般会添加类型校验
				if (array[i].IsString())
				{
					cout << "StringArray[" << i << "] = " << array[i].GetString() << endl;
				}
			}
		}
		//5.4 混合型
		if (doc.HasMember("MixedArray") && doc["MixedArray"].IsArray())
		{
			const rapidjson::Value& array = doc["MixedArray"];
			size_t len = array.Size();
			for (size_t i = 0; i < len; i++)
			{
				//为防止类型不匹配,一般会添加类型校验
				if (array[i].IsString())
				{
					cout << "MixedArray[" << i << "] = " << array[i].GetString() << endl;
				}
				else if (array[i].IsBool())
				{
					cout << "MixedArray[" << i << "] = " << array[i].GetBool() << endl;
				}
				else if (array[i].IsInt())
				{
					cout << "MixedArray[" << i << "] = " << array[i].GetInt() << endl;
				}
				else if (array[i].IsDouble())
				{
					cout << "MixedArray[" << i << "] = " << array[i].GetDouble() << endl;
				}
			}
		}
		//5.5 结构体数组类型
		if (doc.HasMember("People") && doc["People"].IsArray())
		{
			const rapidjson::Value& array = doc["People"];
			size_t len = array.Size();
			for (size_t i = 0; i < len; i++)
			{
				const rapidjson::Value& object = array[i];
				//为防止类型不匹配,一般会添加类型校验
				if (object.IsObject())
				{
					cout << "ObjectArray[" << i << "]: ";
					if (object.HasMember("name") && object["name"].IsString())
					{
						cout << "name=" << object["name"].GetString();
					}
					if (object.HasMember("age") && object["age"].IsInt())
					{
						cout << ", age=" << object["age"].GetInt();
					}
					if (object.HasMember("sex") && object["sex"].IsBool())
					{
						cout << ", sex=" << (object["sex"].GetBool() ? "男" : "女") << endl;
					}
				}
			}
		}
	}

}


//遍历解析
void parse_1()
{
	// 这个是用于遍历json数组,用于不知道name的前提下
	std::string data = "{\"name\":\"qq849635649\",\"age\":20,\"sex\":true}";
	rapidjson::Document dom;
	if (!dom.Parse(data.data()).HasParseError())
	{
		for (rapidjson::Value::ConstMemberIterator iter = dom.MemberBegin(); iter != dom.MemberEnd(); ++iter)
		{
			std::string name = (iter->name).GetString();
			const rapidjson::Value& value = iter->value;
			if (value.IsString())
			{
				cout << name << " : " << value.GetString() << endl;
			}
			else if (value.IsInt())
			{
				cout << name << " : " << value.GetInt() << endl;
			}
			else if (value.IsBool())
			{
				cout << name << " : " << value.GetBool() << endl;
			}
		}
	}
}


int main()
{
//	parse();

	parse_1();


	return 0;
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 RapidJSON 库将一个 JSON 对象添加到另一个 JSON 对象中,可以使用 `AddMember()` 函数。以下是一个示例: ```cpp #include <iostream> #include <rapidjson/document.h> #include <rapidjson/writer.h> #include <rapidjson/stringbuffer.h> using namespace rapidjson; int main() { // 创建一个 JSON 对象 Document document; document.SetObject(); // 创建要添加的 JSON 对象 Value innerObject(kObjectType); Value key("name"); Value value("John"); innerObject.AddMember(key, value, document.GetAllocator()); // 将内部对象添加到外部对象 Value keyOuter("person"); document.AddMember(keyOuter, innerObject, document.GetAllocator()); // 将 JSON 对象转换为字符串 StringBuffer buffer; Writer<StringBuffer> writer(buffer); document.Accept(writer); std::string jsonString = buffer.GetString(); // 打印 JSON 字符串 std::cout << jsonString << std::endl; return 0; } ``` 在上述示例中,我们首先创建了一个外部的 JSON 对象 `document`。然后,我们创建了一个要添加到外部对象中的内部对象 `innerObject`。接下来,我们使用 `AddMember()` 函数将内部对象添加到外部对象中,并指定键名为 `"person"`。最后,我们将整个 JSON 对象转换为字符串形式。 输出结果示例: ```json {"person":{"name":"John"}} ``` 这是一个简单的示例,展示了如何使用 RapidJSON 库将一个 JSON 对象添加到另一个 JSON 对象中。你可以根据需要修改和扩展这个示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值