本节主要介绍RapidJson是如何使用的。

  (1)RapidJson是什么

RapidJson是一个跨平台的c++的json的解析器和生成器;
相比较jsoncpp库,RapidJson只有头文件,容易安装;
RapidJSON 不依赖STL和boost等外部库独立;
只使用如下文件:<cstdio>, <cstdlib>, <cstring>, <inttypes.h>, <new>, <stdint.h>;
高性能,使用模版及内联函数去降低函数调用开销、内部经优化的 Grisu2 及浮点数解析实现、可选的 SSE2/SSE4.2 支持.

  (2)RapidJson使用范例(DOM解析json字符串并修改json中指定元素的值)

#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h" using namespace std;
using namespace rapidjson; int main(int argv ,char *argc[])
{
//1.把JSON解析至DOM
const char * strJson = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":100}";
Document doc;
doc.Parse(strJson);
cout<< strJson << endl;
//2.利用DOM作出修改
Value& v1 = doc["key2"];
v1="value_modify";
//v1.SetString("value_modify");
Value& v2 = doc["key3"];
v2.SetInt(v2.GetInt()+);
//SetString()
//SetBool()
//SetUint()
//SetInt64()
//SetUInt64()
//SetDouble()
//SetFloat()
//SetArray()
//SetObject()
//SetNull() //3.将DOM stringfy 为json
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer); cout<< buffer.GetString() << endl;
return ;
}
{"key1":"value1","key2":"value2","key3":}
{"key1":"value1","key2":"value_modify","key3":}

  (3)文件对象模型(Document Object Model, DOM)API

文件对象模型,在RapidJson中广泛的使用。

    1.构建json value到DOM:

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h" using namespace std;
using namespace rapidjson; int main(int argv ,char *argc[])
{
// 1.准备数据
string name = "MenAngel";
string gender = "boy";
int age = ;
vector<string> hobbys = {"语文","数学","英语"};
map<string,double> score ={{"语文",},{"数学",},{"英语",}}; // 2.初始化DOM
StringBuffer strBuffer;
Writer<StringBuffer> writer(strBuffer);
//2.1 根DOM开始
writer.StartObject();
writer.Key("name");
writer.String(name.c_str());
writer.Key("gender");
writer.String(gender.c_str());
writer.Key("age");
writer.Int(age);
writer.Key("hobby");
writer.StartArray();
for(auto &item : hobbys)
{
writer.String(item.c_str());
}
writer.EndArray();
writer.Key("scores");
writer.StartObject();
for(auto &item : scores)
{
writer.Key((item.first).c_str());
writer.Double(item.second);
}
writer.EndObject();
//2.2 根DOM结束
writer.EndObject(); //3.将上述DOM组织的json数据写入json.txt文件
string outFileName = "json.txt";
ofstream outfile(outFileName,std::ios::trunc);
outfile<<strBuffer.GetString()<<endl;
outfile.flush();
outfile.close();
return ;
}
{"name":"MenAngel","gender":"boy","age":23,"hobbys":["语文","数学","英语"],"scores":{"数学":90.0,"英语":100.0,"语文":80.0}}

    2.构建Json Value到DOM

#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h" using namespace std;
using namespace rapidjson; int main(int argv ,char *argc[])
{
// 1.准备数据
string name = "MenAngel";
string gender = "boy";
int age = ;
vector<string> hobbys = {"语文","数学","英语"};
map<string,double> scores ={{"语文",},{"数学",},{"英语",}}; //2.初始化DOM
Document doc;
Document::AllocatorType& allocator = doc.GetAllocator();
doc.SetObject();//实例化一个GenericValue到根DOM
Value tempValue1;
tempValue1.SetString(name.c_str(),allocator);
doc.AddMember("name",tempValue1,allocator);
Value tempValue2(rapidjson::kObjectType);
tempValue2.SetString(gender.c_str(),allocator);
doc.AddMember("gender",tempValue2,allocator);
doc.AddMember("age",age,allocator);
Value tempValue3(kArrayType);
for(auto hobby:hobbys)
{
Value hobbyValue(kStringType);
hobbyValue.SetString(hobby.c_str(),allocator);
tempValue3.PushBack(hobbyValue,allocator);
}
doc.AddMember("hobbys",tempValue3,allocator);
Value tempValue4(kObjectType);
tempValue4.SetObject();
for(auto score : scores)
{
Value scoreValue(kNumberType);
//cout<<score.second;
scoreValue.SetInt(score.second);
Value scoreName(kStringType);
scoreName.SetString(score.first.c_str(),allocator);
tempValue4.AddMember(scoreName,scoreValue,allocator);
}
doc.AddMember("scores",tempValue4,allocator);
StringBuffer strBuffer;
Writer<StringBuffer> writer(strBuffer);
doc.Accept(writer); string outFileName = "json.txt";
ofstream outfile(outFileName,std::ios::trunc);
outfile<<strBuffer.GetString()<<endl;
outfile.flush();
outfile.close();
return ;
}
{"name":"MenAngel","gender":"boy","age":,"hobbys":["语文","数学","英语"],"scores":{"数学":90.0,"英语":100.0,"语文":80.0}}

    3.RapidJSon查询

#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson; int main(int argv,char *argc[])
{
//1.构建json
const char * strJson = "{\"name\":\"MenAngel\",\"age\":23,\"hobbys\":[\"语文\",\"数学\",\"英语\"]}";
Document doc;
doc.Parse(strJson);
StringBuffer strBuffer;
Writer<StringBuffer> writer(strBuffer);
doc.Accept(writer);
cout<<strBuffer.GetString()<<endl;
//2.查询json值
cout<<"遍历方法一:"<<endl;
int count = doc.MemberCount();
cout<<"doc 的属性成员有 "<<count<<"个!"<<endl;
static const char* kTypeNames[] =
{ "Null", "False", "True", "Object", "Array", "String", "Number" };
int i = ;
for(Value::MemberIterator iter = doc.MemberBegin();iter != doc.MemberEnd();++iter)
{
cout<<++i<<".property "<<iter->name.GetString()<<" is "<<kTypeNames[iter->value.GetType()]<<endl;
if(iter->value.GetType() == )
{
//3.遍历Array
for(auto &item : iter->value.GetArray())
//IsBool、IsObject、IsInt、IsNull、IsNumber、IsDouble
if(item.IsString())
cout<<"item = "<<item.GetString()<<endl;
}
}
//4.查询某个成员是否存在
Value::ConstMemberIterator it = doc.FindMember("scores");
if(it != doc.MemberEnd())
cout<<"Has Finded!"<<endl;
else
cout<<"Not Finded!"<<endl;
//5.遍历doc的所有成员
cout<<"遍历方法二:"<<endl;
for(auto &m :doc.GetObject())
{
cout<<"Has member :"<<m.name.GetString()<<" = ";
if(m.value.IsString())
cout<<m.value.GetString()<<endl;
if(m.value.IsInt())
cout<<m.value.GetInt()<<endl;
if(m.value.IsBool())
cout<<m.value.GetBool()<<endl;
if(m.value.IsArray())
cout<<"is array"<<endl;
}
return ;
}
{"name":"MenAngel","age":,"hobbys":["语文","数学","英语"]}
遍历方法一:
doc 的属性成员有 3个!
.property name is String
.property age is Number
.property hobbys is Array
item = 语文
item = 数学
item = 英语
Not Finded!
遍历方法二:
Has member :name = MenAngel
Has member :age =
Has member :hobbys = is array

    4.RapidJson属性获取

#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson; int main(int argv,char *argc[])
{
//1.构建json
const char * strJson = "{\"name\":\"MenAngel\",\"age\":23,\"hobbys\":[\"语文\",\"数学\",\"英语\"]}";
Document doc;
doc.Parse(strJson);
StringBuffer strBuffer;
Writer<StringBuffer> writer(strBuffer);
doc.Accept(writer);
cout<<strBuffer.GetString()<<endl;
//2.DOM根是一个对象
if(doc.IsObject())
cout<<"doc is object!"<<endl;
//3.doc不为j空
if(!doc.IsNull())
{
cout<<"doc is not null!"<<endl;
}
Document document;
if(document.IsNull())
{
cout<<"document is null!"<<endl;
cout<<"Set Object"<<endl;
document.SetObject();
}
if(!document.IsNull())
cout<<"document is not null!"<<endl;
//4.DOM 的大小.
cout<<"doc.MemmberCount() = "<<doc.MemberCount()<<endl;
//5.增加HasMember判断防止断言错误
if(doc.HasMember("hobbys") && !doc["hobbys"].Empty())
cout<<"doc[\"hobbys\"] is not empty!"<<endl;
else
cout<<"member not exits!"<<endl;
//6.取键值
Value::ConstMemberIterator iter = doc.FindMember("age");
if(iter != doc.MemberEnd())
{
cout<<"Member age is exits!"<<endl;
cout<<iter->name.GetString()<<":"<<iter->value.GetInt()<<endl;
}
//7.Array的大小
cout<<"doc[\"hobbys\"].Capacity() = "<<doc["hobbys"].Capacity()<<endl;
//8.字符串的大小 当字符中存在\u0000时strlen会到此截断
cout<<"doc[\"name\"].length = "<<strlen(doc["name"].GetString())<<endl;
cout<<"doc[\"name\"].length = "<<doc["name"].GetStringLength()<<endl;
doc.AddMember("test","a\u0000b",doc.GetAllocator());
//只能当test成员存在时才能直接赋值
//doc["test"] = "a\u0000b";
cout<<"doc[\"test\"].length = "<<strlen(doc["test"].GetString())<<endl;
cout<<"doc[\"test\"].length = "<<doc["test"].GetStringLength()<<endl;
//
return ;
}
{"name":"MenAngel","age":,"hobbys":["语文","数学","英语"]}
doc is object!
doc is not null!
document is null!
Set Object
document is not null!
doc.MemmberCount() =
doc["hobbys"] is not empty!
Member age is exits!
age:
doc["hobbys"].Capacity() =
doc["name"].length =
doc["name"].length =
doc["test"].length =
doc["test"].length =

    5.RapidJson一些特性

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson; int main(int argv,char *argc[])
{
//1.构建json
const char * strJson = "{\"name\":\"MenAngel\",\"age\":23,\"hobbys\":[\"语文\",\"数学\",\"英语\"]}";
Document doc;
doc.Parse(strJson);
StringBuffer strBuffer;
Writer<StringBuffer> writer(strBuffer);
doc.Accept(writer);
cout<<strBuffer.GetString()<<endl;
//2.const char *、string、value(kStringType)之间比较大小
if(doc["name"] == "MenAngel")
cout<<"(1)doc[\"name\"] equal MenAngel!"<<endl;
if(doc["name"].GetString() == string("MenAngel"))
cout<<"(2)doc[\"name\"] equal MenAngel!"<<endl;
if( == strcmp(doc["name"].GetString(),"MenAngel"))
cout<<"(3)doc[\"name\"] equal MenAngel!"<<endl;
if(doc["name"].GetString() != "MenAngel")
cout<<"(4)不成立!"<<endl;
//3.value赋值
Value v;//v is NULL && v is not Object
if(v.IsNull())
cout<<"v is Null()"<<endl;
v.SetObject();
if(v.IsObject())
cout<<"v is Object"<<endl;
Value tValue(kObjectType);//tValue is not Null,tValue is Object
if(!tValue.IsNull())
cout<<"tVlaue is not NULL!"<<endl;
if(tValue.IsObject())
cout<<"tValue is Object"<<endl;
//4.转移语义,避免value深赋值
//AddMember(), PushBack()、赋值均采用深赋值
Value a();
Value b();
a = b;
if(b.IsNull())
cout<<"b is Null,"<<"a = "<<a.GetInt()<<endl;
Value c(kArrayType);
Document::AllocatorType& allocator = doc.GetAllocator();
c.PushBack(Value(), allocator);
c.PushBack(Value().SetInt(), allocator); // fluent API
c.PushBack(Value().Move(), allocator); // 转移语义
cout<<"c.Capacity() = "<<c.Capacity()<<endl;
cout<<"c.Size() = "<<c.Size()<<endl;
for(auto &m : c.GetArray())
{
cout<<"item = "<<m.GetInt()<<endl;
}
//字符串的容量及大小
cout<<"doc[\"hobbys\"].Size() = "<<doc["hobbys"].Size()<<endl;
cout<<"doc[\"hobbys\"].Capacity() = "<<doc["hobbys"].Capacity()<<endl;
//5.交换value
Value d();
Value e("string");
d.Swap(e);
if(!d.IsNull() && !e.IsNull())
{
cout<<"d is not null,e is not null!"<<endl;
cout<<"d = "<<d.GetString()<<" e = "<<e.GetInt()<<endl;
}
//6.创建string
//复制字符串
Value f(kStringType);
char str[] = "My first string!";
f.SetString(str,doc.GetAllocator());
cout<<f.GetString()<<endl;
Value g;
char buffer[];
int len = sprintf(buffer, "%s -> %s", "name", "value"); // 动态创建的字符串。
g.SetString(buffer,len,doc.GetAllocator());
//g.SetString(buffer,doc.GetAllocator())
cout<<g.GetString()<<endl;
//简单引用常量字符串
Value h;
h.SetString("My third String!");// h = "My thrid String!"
cout<<h.GetString()<<endl;
//简单引用常量l字符串
Value i;
const char * tempStr = "my fourth string";
size_t cstr_len = strlen(tempStr);
i.SetString(StringRef(tempStr));
//i.SetString(StringRef(tempStr,cstr_len));
//i = StringRef(tempStr,cstr_len)
//i = StringRef(tempStr)
//i.SetString(tempStr); 不合法
cout<<i.GetString()<<endl;
return ;
}
{"name":"MenAngel","age":,"hobbys":["语文","数学","英语"]}
()doc["name"] equal MenAngel!
()doc["name"] equal MenAngel!
()doc["name"] equal MenAngel!
()不成立!
v is Null()
v is Object
tVlaue is not NULL!
tValue is Object
b is Null,a =
c.Capacity() =
c.Size() =
item =
item =
item =
doc["hobbys"].Size() =
doc["hobbys"].Capacity() =
d is not null,e is not null!
d = string e =
My first string!
name -> value
My third String!
my fourth string

最新文章

  1. poj3422 Kaka&#39;s Matrix Travels(最小费用最大流问题)
  2. 【SQL】小心字符串拼接导致长度爆表
  3. LayoutInflater的实例化
  4. loadrunner之Paramater在负载测试中的数据生成规则
  5. execvp使用实例
  6. 关于MySQL的分区(partion)
  7. margin:0 auto 与 text-align:center 的区别(转载)
  8. 写一个根据id字段查找记录的缓存函数(javascript)
  9. ORM 实现数据库表的增删改查
  10. R语言中函数调试
  11. 为多态基类声明virtual析构函数
  12. springboot~让我习惯了TDD的开发模式
  13. ASP.Net Core2.1中的HttpClientFactory系列二:集成Polly处理瞬态故障
  14. ArcGIS案例学习笔记-点集中最近点对和最远点对
  15. Linux内核分析——第五周学习笔记
  16. 数据库实例: STOREBOOK &gt; 表空间 &gt; 编辑 表空间: TEMP
  17. java类过滤器,防止页面SQL注入
  18. Python3.2官方文档翻译--迭代器
  19. python多线程锁lock/Rlock/BoundedSemaphore/Condition/Event
  20. EM算法和GMM模型推导

热门文章

  1. 洛谷 P1939 矩阵加速(数列)
  2. STL 队列
  3. c# 将dwg文件转化为pdf
  4. 理解-NumPy
  5. Shell总结1
  6. Java描述表达式求值的两种解法:双栈结构和二叉树
  7. Vuex模块化
  8. Spring Boot MyBatis 数据库集群访问实现
  9. Http协议4个新的http状态码:428、429、431、511;
  10. Codeforces 1006F