您的位置:首页 > Web前端 > JavaScript

rapidjson使用样例

2016-12-30 10:54 411 查看


rapidjson库解析测试代码

rapidjson默认支持的字符格式是utf-8的,一般中间接口是json文件的话存储为utf-8比较通用一些。

如果是unicode的需要转换。但从源码中的ch类型看,应该是支持泛型的,具体在用到了可以仔细研究一下。

这篇文档中有json解析相关库的性能比较,rapidjson还是各方面均衡比较突出的。https://github.com/miloyip/nativejson-benchmark


库下载

官网:http://rapidjson.org/

下载:https://github.com/miloyip/rapidjson/
文件包含



代码样例

#include "..\..\3rd\include\rapidjson\document.h"
#include "..\..\3rd\include\rapidjson\writer.h"
#include "..\..\3rd\include\rapidjson\stringbuffer.h"
#include <iostream>

using namespace rapidjson;

void rapidjson_test_char()
{
// 1. Parse a JSON string into DOM.
const char* json1 = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json1);

// 2. Modify it by DOM.
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);

// 3. Stringify the DOM
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);

// Output {"project":"rapidjson","stars":11}

std::cout << buffer.GetString() << std::endl;

printf("\n");
//解析第二个例子
//{"dictVersion": 1,"key": "word","value": "test"}
const char* json2 = "{\"dictVersion\":1,\"key\":\"word\",\"value\":\"test\"}";
d.Parse(json2);
if (d.HasParseError())
{
printf("Parse error!!\n");
return ;
}

if(d.HasMember("dictVersion"))
{
printf("The dictVersion is:%d\n", d["dictVersion"].GetInt());
}

if(d.HasMember("key"))
{
printf("The key is:%s\n", d["key"].GetString());
}

if(d.HasMember("value"))
{
printf("The value is:%s\n", d["value"].GetString());
}
}


样例测试结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json rapidjson 源码