rapidjson的使用例子

最近在使用json, 由以前的jsoncpp转为rapidjson, 听说这个效率高一点;

不过我觉得比较好的就是rapidjson不需要库,只要头文件就可以了

记录下来方便自己以后使用

#if 1
#include <string>
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>


using namespace rapidjson;
using namespace std;

int main(int argc, char *argv[])
{
	Document document;
	document.SetObject();
	Document::AllocatorType& allocator = document.GetAllocator();

	Value array(kArrayType);

	for (int i = 0; i < 2; i++)
	{
		Value object(kObjectType);
		object.AddMember("id", 1, allocator);
		object.AddMember("name", "test", allocator);
		object.AddMember("version", 1.01, allocator);
		object.AddMember("vip", true, allocator);
		array.PushBack(object, allocator);
	}

	document.AddMember("title", "PLAYER INFO", allocator);
	document.AddMember("players", array, allocator);
	Value age(kArrayType);
	for (int j = 0; j < 5;j++)
	{
		Value object(kNumberType);
		object.SetInt(j);
		age.PushBack(object, allocator);
	}

	document.AddMember("age",age,allocator);

	StringBuffer buffer;
	Writer<StringBuffer> writer(buffer);
	document.Accept(writer);
	const char * out = buffer.GetString();
	cout << out << endl;

	cout << "-------------analyze--------------" << endl;
	/*
	{
		"title":"PLAYER INFO",
		"players":[
			{
				"id":1,
				"name":"test",
				"version":1.01,
				"vip":true
			},
			{
				"id":1,
				"name":"test",
				"version":1.01,
				"vip":true
			}
		],
		"age":[0,1,2,3,4]
	}
	*/
	Document doc1;
	doc1.Parse<0>(out);               ///< 通过Parse方法将Json数据解析出来
	if (doc1.HasParseError())
	{
		cout << "GetParseError%s\n" << doc1.GetParseError() << endl;
		return 1;
	}
	rapidjson::Value & valString = doc1["title"];
	if (valString.IsString())
	{
		const char * str = valString.GetString();
		cout << "title:"<<str << endl;
	}

	rapidjson::Value & valAge = doc1["age"];
	if (valAge.IsArray())
	{
		cout << "age:" << endl;
		for (int i = 0; i < valAge.Capacity(); ++i)
		{
			rapidjson::Value & val = valAge[i];
			cout << val.GetInt() << endl;
		}
	}

	rapidjson::Value & players = doc1["players"];
	if (players.IsArray())
	{
		cout << "players:" << endl;
		for (int i = 0; i < players.Capacity(); ++i)
		{
			rapidjson::Value & val = players[i];
			cout << "id:" << val["id"].GetInt() << endl;
			cout << "name:" << val["name"].GetString() << endl;
			cout << "version:" << val["version"].GetDouble() << endl;
			cout << "vip:" << val["vip"].GetBool() << endl;
		}
	}
	return 0;
}


好的,以下是一个使用 RapidJSON 库的完整例子,包括解析 JSON 字符串、使用 JSON 对象设置成员、获取 JSON 数据的过程。 ```c++ #include <iostream> #include <string> #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" using namespace std; using namespace rapidjson; int main() { // 解析 JSON 字符串 string json_str = "{\"name\": \"Tom\", \"age\": 20, \"is_student\": true, \"scores\": [85, 90, 95]}"; Document json_obj; json_obj.Parse(json_str.c_str()); // 使用 JSON 对象设置成员 Value email_val; email_val.SetString("tom@example.com", json_obj.GetAllocator()); json_obj.AddMember("email", email_val, json_obj.GetAllocator()); // 获取 JSON 数据 string name = json_obj["name"].GetString(); int age = json_obj["age"].GetInt(); bool is_student = json_obj["is_student"].GetBool(); const Value& scores = json_obj["scores"]; if (scores.IsArray()) { cout << "Scores: "; for (SizeType i = 0; i < scores.Size(); ++i) { cout << scores[i].GetInt() << " "; } cout << endl; } cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Is student: " << boolalpha << is_student << endl; cout << "Email: " << json_obj["email"].GetString() << endl; // 将 JSON 对象序列化为字符串 StringBuffer buffer; Writer<StringBuffer> writer(buffer); json_obj.Accept(writer); cout << "Serialized JSON: " << buffer.GetString() << endl; return 0; } ``` 输出结果: ``` Scores: 85 90 95 Name: Tom Age: 20 Is student: true Email: tom@example.com Serialized JSON: {"name":"Tom","age":20,"is_student":true,"scores":[85,90,95],"email":"tom@example.com"} ``` 注意:在使用 RapidJSON 的过程中,需要注意内存管理和错误处理。RapidJSON 使用 C++11 的特性,因此需要使用支持 C++11 的编译器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值