rapidjson库的使用

C++下最好的json库必然是MiloYip大神写的rapidjson库啦~   连cocos2dx官方都把这个库集成进去了

1 两个问题

(1)标准json和非标准json:

标准json要求键必须都是双引号的字符串,而非标准json可以单引号。

例如:

{a : 'abc'}

{'a' : 'abc'}

{a : "abc"}

{"a" : "abc"}

只有第4个是标准json

(2)json中的[]与{}:

在 JSON 里 [] 是 Array {} 是Ojbect

[] Array 的key 是 int  从0算起
{} 的key 是 string

var a= Array();
a[a.length] = '3';
a[a.length] = '4';
a[a.length] = '5';

a toJSON 后 ='["3", "4", "5"]'

var a = new Object();

a['test1'] = '3';
a['test2'] = '4';
a['test3'] = '5';

a toJSON 后 = '{"test1":"3", "test2":"4", "test3":"5"}'

rapidjson读写测试

下载rapidjson库,解压后关联到工程。

代码:

#include <iostream>
#include <string>
#include <fstream>
//包含rapidjson必要头文件,rapidjson文件夹拷贝到工程目录,或者设置include路径,或者加入到工程树
#include "rapidjson/document.h"
#include "rapidjson/filestream.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;  //引入rapidjson命名空间

//写json文件
void json_write()
{
	Document doc;
	doc.SetObject();
	Document::AllocatorType &allocator=doc.GetAllocator(); //获取分配器
	//1.添加字符串对象
	doc.AddMember("author","tashaxing",allocator); 
	//2.添加数组对象
	Value array1(kArrayType);
	for(int i=0;i<3;i++)
	{
		Value int_object(kObjectType);
		int_object.SetInt(i);
		array1.PushBack(int_object,allocator);
	}
	doc.AddMember("number",array1,allocator);
	//3.添加复合对象
	Value object(kObjectType);
	object.AddMember("language1","C++",allocator);
	object.AddMember("language2","java",allocator);
	doc.AddMember("language",object,allocator);
	//4.添加对象数组和复合对象的组合
	Value array2(kArrayType);
	Value object1(kObjectType);
	object1.AddMember("hobby","drawing",allocator);
	array2.PushBack(object1,allocator);
	Value object2(kObjectType);
	object2.AddMember("height",1.71,allocator);
	array2.PushBack(object2,allocator);
	doc.AddMember("information",array2,allocator);
	StringBuffer buffer;
	PrettyWriter<StringBuffer> pretty_writer(buffer);  //PrettyWriter是格式化的json,如果是Writer则是换行空格压缩后的json
	doc.Accept(pretty_writer);
	//打印到屏幕
	cout<<"the json output:"<<endl;
	cout<<buffer.GetString()<<endl;
	//输出到文件
	ofstream fout;
	fout.open("test");    //可以使绝对和相对路径,用\\隔开目录,test, test.json, test.txt 都行,不局限于文件格式后缀,只要是文本文档
	fout<<buffer.GetString();
	fout.close();
}

//读json文件
void json_read()
{
	cout<<"the json read:"<<endl;
	ifstream fin;
	fin.open("test");
	string str;
	string str_in="";
	while(getline(fin,str))    //一行一行地读到字符串str_in中
	{
		str_in=str_in+str+'\n';
	}
	//解析并打印出来
	Document document;
	document.Parse<0>(str_in.c_str());

	Value &node1=document["author"];
	cout<<"author: "<<node1.GetString()<<endl;

	Value &node2=document["number"];
	cout<<"number: "<<endl;
	if(node2.IsArray())
	{
		for(int i=0;i<node2.Size();i++)
			cout<<'\t'<<node2[i].GetInt()<<endl;
	}

	Value &node3=document["language"];
	cout<<"language: "<<endl;
	Value &tmp=node3["language1"];
	cout<<'\t'<<"language1: "<<tmp.GetString()<<endl;
	tmp=node3["language2"];
	cout<<'\t'<<"language2: "<<tmp.GetString()<<endl;

	Value &node4=document["information"];
	cout<<"information: "<<endl;
	if(node4.IsArray())
	{
		int i=0;
		Value &data=node4[i];   //注意,此处下表索引只能用变量,不能用常量,例如node[0]编译错误
		cout<<'\t'<<"hobby: "<<data["hobby"].GetString()<<endl;
		i=1;
		data=node4[i];
		cout<<'\t'<<"height: "<<data["height"].GetDouble()<<endl;
	}

}
int main(int argc,char **argv)
{
	//写、读 测试
	json_write();
	json_read();
	return 0;
}


输出的json:

{
    "author": "tashaxing",
    "number": [
        0,
        1,
        2
    ],
    "language": {
        "language1": "C++",
        "language2": "java"
    },
    "information": [
        {
            "hobby": "drawing"
        },
        {
            "height": 1.71
        }
    ]
}


如果有中文的话,建议转成utf8输出到文件。


 

高效的 C++ JSON 解析/生成器,提供 SAX 及 DOM 风格 API 简介 RapidJSON 是一个 C++JSON 解析器及生成器。它的灵感来自RapidXml 。 RapidJSON 小而全。它同时支持 SAX 和 DOM 风格的 API。SAX 解析器只有约 500 行代码。 RapidJSON 快。它的性能可与 strlen() 相比。可支持 SSE2/SSE4.2 加速。 RapidJSON 独立。它不依赖于 BOOST 等外部。它甚至不依赖于 STL。 RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。 RapidJSON 对 Unicode 友好。它支持 UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON 可以在分析一个 UTF-8 文件至 DOM 时,把当中的 JSON 字符串转码至 UTF-16。它也支持代理对(surrogate pair)及 "\u0000"(空字符)。 JSON(JavaScript Object Notation)是一个轻量的xx交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404,并支持可选的放宽语法。 一、使用说明         rapidjson.fne 为英文原版         rapidjson_cn.fne  为中文翻译版(翻译的中文命令有点糙),使用中文版时,请改文件名为rapidjson.fne 不然会出错的。         rapidjson_static.lib 为静态,部分中文英文。         中文和英文可以无缝切换,直接替换支持文件就可以了。         本支持由VS2017,所以编译时,也必须要用VS2017编译。         VC2017连接器下载地址: http://bbs.eyuyan.com/read.php?tid=410252   VS2017易支持模板+VC2017链接器(讨厌vc6的可看)-->hxznhf http://bbs.eyuyan.com/read.php?tid=408541   全易论坛独创首发,Vs2017Linker编译器 -->cs666         关于RapidJSON的相关问题,请看:http://rapidjson.org/zh-cn/md_doc_faq_8zh-cn.html         使用有声明问题请加QQ群: 心宇->EVAxx研究中心(255829517)         现在支持完善了大概80%左右,已经可以正常使用,剩下20%是关于 reader 和编码以及一些参数有关,暂时对易不是很重要,后面也会完善。 二、版权声明         本支持为封装TX开源项目 RapidJSON ,为解决易语言 没有高性能JSON的问题。         项目官网:http://rapidjson.org/zh-cn/         github:https://github.com/Tencent/rapidjson/
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值