void rapidjson1(){ rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s); writer.StartObject(); // Between StartObject()/EndObject(),
writer.Key("hello"); // output a key,
writer.String("world"); // follow by a value.
writer.Key("t");
writer.Bool(true);
writer.Key("f");
writer.Bool(false);
writer.Key("n");
writer.Null();
writer.Key("i");
writer.Uint(123);
writer.Key("pi");
writer.Double(3.1416);
writer.Key("a");
writer.StartArray(); // Between StartArray()/EndArray(),
for (unsigned i = 0; i < 4; i++)
writer.Uint(i); // all values are elements of the array.
writer.EndArray();
writer.EndObject();
// {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]}
std::cout << s.GetString() << std::endl;
}

document.json

{
"name": "xiaoming",
"gender": "boy",
"hobby": [
"足球",
"篮球",
"电影"
],
"score": {
"数学": 91.5,
"语文": 95.5,
"英语": 96
},
"lover": {
"name": "xiaohong",
"gender": "girl",
"hobby": [
"画画",
"跳舞",
"唱歌"
],
"score": {
"数学": 78.5,
"语文": 89,
"英语": 90
}
}
}
void rapidjson2(){

    std::ifstream t("/home/leoxae/document1.json");
// std::ifstream t("/home/leoxae/CLionProjects/KeekoAIRobot/data/ninstructionRecognition/bcb2_feature.json");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>()); rapidjson::Document document;
document.Parse(str.c_str()); rapidjson::Value::ConstMemberIterator iter = document.FindMember("name");
if(iter != document.MemberEnd()){
cout << "name : " << iter->value.GetString() << endl;
} iter = document.FindMember("gender");
if(iter != document.MemberEnd()){
cout << "gender : " << iter->value.GetString() << endl;
} if(document.HasMember("hobby")){
cout << "hobby : " << endl;
const rapidjson::Value& childValue = document["hobby"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetString() << endl;
}
} if(document.HasMember("score")){
cout << "score : " << endl;
const rapidjson::Value& childIter = document["score"];
for(rapidjson::Value::ConstMemberIterator it = childIter.MemberBegin(); it != childIter.MemberEnd(); ++it){
cout << " " << it->name.GetString() << " : " << it->value.GetDouble() << endl;
}
} if(document.HasMember("lover")){
cout << "lover : " << endl;
const rapidjson::Value& chileValue = document["lover"];
rapidjson::Value::ConstMemberIterator chileIter = chileValue.FindMember("name");
if(chileIter != chileValue.MemberEnd()){
cout << " " << "name : " << chileIter->value.GetString() << endl;
} chileIter = chileValue.FindMember("gender");
if(chileIter != chileValue.MemberEnd()){
cout << " " << "gender : " << chileIter->value.GetString() << endl;
} if(chileValue.HasMember("hobby")){
cout << " " << "hobby : " << endl;
const rapidjson::Value& chile2Value = chileValue["hobby"];
for(rapidjson::SizeType i = 0; i < chile2Value.Size(); ++i){
cout << " " << chile2Value[i].GetString() << endl;
}
} if(chileValue.HasMember("score")){
cout << " " << "score : " << endl;
const rapidjson::Value& child2Iter = chileValue["score"];
for(rapidjson::Value::ConstMemberIterator it = child2Iter.MemberBegin(); it != child2Iter.MemberEnd(); ++it){
cout << " " << it->name.GetString() << " : " << it->value.GetDouble() << endl;
}
}
} }

ninstr.json

{"\u6269\u5c551": [0, 592],
"\u6269\u5c552": [592, 1232],
"\u6269\u5c553": [1232, 2073],
"\u6269\u5c554": [2073, 2874],
"\u6269\u5c555": [2874, 3786],
"\u6269\u5c556": [3786, 4683],
"\u6269\u5c557": [4683, 5267],
"\u6269\u5c558": [5267, 6151],
"\u6269\u5c559": [6151, 6895],
"\u6269\u5c5510": [6895, 7694],
"\u5730\u57ab\u524d\u8fdb": [7694, 8790],
"\u5730\u57ab\u5de6\u8f6c": [8790, 10237],
"\u5730\u57ab\u53f3\u8f6c": [10237, 11617],
"P1": [11617, 12809], "P2": [12809, 13680],
"\u5f00\u59cb": [13680, 15049],
"\u7ed3\u675f": [15049, 16417],
"\u5730\u57ab\u524d\u8fdb\u5c0f": [16417, 17113],
"\u5730\u57ab\u5de6\u8f6c\u5c0f": [17113, 18433],
"\u5730\u57ab\u53f3\u8f6c\u5c0f": [18433, 19954],
"\u5f00\u59cb\u5c0f": [19954, 21242],
"\u7ed3\u675f\u5c0f": [21242, 22571],
"\u6682\u505c": [22571, 23785]}
void rapidjson3(){

//    std::ifstream t("/home/leoxae/document.json");
// std::ifstream t("/home/leoxae/CLionProjects/KeekoAIRobot/data/ninstructionRecognition/bcb2_feature.json");
std::ifstream t("/home/leoxae/ninstr.json");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>()); rapidjson::Document document;
document.Parse(str.c_str()); //扩展1
if(document.HasMember("扩展1")){
cout << "扩展1 : " << endl;
const rapidjson::Value& childValue = document["扩展1"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
} //扩展2
if(document.HasMember("扩展2")){
cout << "扩展2 : " << endl;
const rapidjson::Value& childValue = document["扩展2"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展3
if(document.HasMember("扩展3")){
cout << "扩展3 : " << endl;
const rapidjson::Value& childValue = document["扩展3"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展4
if(document.HasMember("扩展4")){
cout << "扩展4 : " << endl;
const rapidjson::Value& childValue = document["扩展4"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展5
if(document.HasMember("扩展5")){
cout << "扩展5 : " << endl;
const rapidjson::Value& childValue = document["扩展5"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展6
if(document.HasMember("扩展6")){
cout << "扩展6 : " << endl;
const rapidjson::Value& childValue = document["扩展6"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展7
if(document.HasMember("扩展7")){
cout << "扩展7 : " << endl;
const rapidjson::Value& childValue = document["扩展7"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展8
if(document.HasMember("扩展8")){
cout << "扩展8 : " << endl;
const rapidjson::Value& childValue = document["扩展8"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展9
if(document.HasMember("扩展9")){
cout << "扩展9 : " << endl;
const rapidjson::Value& childValue = document["扩展9"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//扩展10
if(document.HasMember("扩展10")){
cout << "扩展10 : " << endl;
const rapidjson::Value& childValue = document["扩展10"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//地垫前进
if(document.HasMember("地垫前进")){
cout << "地垫前进 : " << endl;
const rapidjson::Value& childValue = document["地垫前进"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//地垫左转
if(document.HasMember("地垫左转")){
cout << "地垫左转 : " << endl;
const rapidjson::Value& childValue = document["地垫左转"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//地垫右转
if(document.HasMember("地垫右转")){
cout << "地垫右转 : " << endl;
const rapidjson::Value& childValue = document["地垫右转"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//P1
if(document.HasMember("P1")){
cout << "P1 : " << endl;
const rapidjson::Value& childValue = document["P1"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//P2
if(document.HasMember("P2")){
cout << "P2 : " << endl;
const rapidjson::Value& childValue = document["P2"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//开始
if(document.HasMember("开始")){
cout << "开始 : " << endl;
const rapidjson::Value& childValue = document["开始"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//结束
if(document.HasMember("结束")){
cout << "结束 : " << endl;
const rapidjson::Value& childValue = document["结束"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//地垫前进小
if(document.HasMember("地垫前进小")){
cout << "地垫前进小 : " << endl;
const rapidjson::Value& childValue = document["地垫前进小"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//地垫左转小
if(document.HasMember("地垫左转小")){
cout << "地垫左转小 : " << endl;
const rapidjson::Value& childValue = document["地垫左转小"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//地垫右转小
if(document.HasMember("地垫右转小")){
cout << "地垫右转小 : " << endl;
const rapidjson::Value& childValue = document["地垫右转小"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//开始小
if(document.HasMember("开始小")){
cout << "开始小 : " << endl;
const rapidjson::Value& childValue = document["开始小"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//结束小
if(document.HasMember("结束小")){
cout << "结束小 : " << endl;
const rapidjson::Value& childValue = document["结束小"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
//暂停
if(document.HasMember("暂停")){
cout << "暂停 : " << endl;
const rapidjson::Value& childValue = document["暂停"];
for(rapidjson::SizeType i = 0; i < childValue.Size(); ++i){
cout << " " << childValue[i].GetInt() << endl;
}
}
}
void readrapidjson(){
string strJsonTest = "{\"item_1\":\"value_1\",\"item_2\":\"value_2\",\"item_3\":\"value_3\",\"item_4\":\"value_4\",\"item_arr\":[\"arr_vaule_1\",\"arr_vaule_2\"]}";
rapidjson::Document docTest;
docTest.Parse<0>(strJsonTest.c_str()); if (!docTest.HasParseError())
{
for (rapidjson::Value::ConstMemberIterator itr = docTest.MemberBegin(); itr != docTest.MemberEnd(); itr++)
{
rapidjson::Value jKey;
rapidjson::Value jValue;
rapidjson::Document::AllocatorType allocator;
jKey.CopyFrom(itr->name, allocator);
jValue.CopyFrom(itr->value,allocator);
if (jKey.IsString())
{
string name = jKey.GetString();
// printf("\r\nname: %s\r\n",name.c_str());
cout << "name=>>:" << name << endl;
}
}
} }

rapidjson解析与构造实例的更多相关文章

  1. Cocos2d-X网络编程(5) 使用Rapidjson解析数据

    Json基础及28种c++解析库性能对比 JSON 概念和特点:     JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)     JSON ...

  2. 在SQL中使用CLR提供基本函数对二进制数据进行解析与构造

      二进制数据包的解析一般是借助C#等语言,在通讯程序中解析后形成字段,再统一单笔或者批量(表类型参数)提交至数据库,在通讯程序中,存在BINARY到struct再到table的转换. 现借助CLR提 ...

  3. SAX方式解析XML文件实例

    books.XML文件: 书籍book.java实体类: public class Book { private String id; private String name; private Str ...

  4. 两种库解析、构造 JSON

    1.用CJSON库 1.1解析Json 需要解析的JSON文件: { "name":"Tsybius", , "sex_is_male":t ...

  5. dom4j解析xml字符串实例

    DOM4J 与利用DOM.SAX.JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异.功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml ...

  6. 小白详细解析C#反射特性实例

    套用MSDN上对于反射的定义:反射提供了封装程序集.模块和类型的对象(Type 类型).可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性.如果代 ...

  7. 【零基础】AI神经元解析(含实例代码)

    一.序言 关于“深度学习”大部分文章讲的都云里雾里,直到看到“床长”的系列教程以及<深度学习入门:基于Python的理论与实现>,这里主要是对这两个教程进行个人化的总结,目标是让“0基础” ...

  8. Request 接收参数乱码原理解析三:实例分析

    通过前面两篇<Request 接收参数乱码原理解析一:服务器端解码原理>和<Request 接收参数乱码原理解析二:浏览器端编码原理>,了解了服务器和浏览器编码解码的原理,接下 ...

  9. 【转】adns解析库——域名解析实例(C++、linux)

    转自:http://blog.csdn.net/fty8788/article/details/7480334 adns是一个开源的dns解析库 官方文档:http://www.chiark.gree ...

随机推荐

  1. Maven 学习第一步[转载]

    转载至:http://www.cnblogs.com/haippy/archive/2012/07/04/2576453.html 什么是 Maven?(摘自百度百科) Maven是Apache的一个 ...

  2. linux允许直接以root身份ssh登录

    1. sudo su - 2. vim /etc/ssh/sshd_config 3. let "PermitRootLogin" equal yes 4. :wq 5. serv ...

  3. iOS11&IPhoneX适配

    1.在iOS 11中,会默认开启获取的一个估算值来获取一个大体的空间大小,导致不能正常显示,可以选择关闭.目前尝试在delegate中处理不能很好的解决,不过可以直接设置: Swift if #ava ...

  4. JavaIO——File类

    1.File文件类 File类(描述具体文件或文件夹的类):是唯一一个与文件本身操作有关的程序类,可完成文件的创建.删除.取得文件信息等操作.但不能对文件的内容进行修改. (1)File类的基本使用 ...

  5. Template Metaprogramming in C++

    说实话,学习C++以来,第一次听说"Metaprogramming"这个名词. Predict the output of following C++ program. 1 #in ...

  6. 编译安装redis之快速增加redis节点

    #: 下载安装包 [root@localhost ~]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz #:解压 [root@l ...

  7. oracle 存储过程及REF CURSOR的使用

    基本使用方法及示例 1.基本结构: CREATE OR REPLACE PROCEDURE 存储过程名字 (参数1 IN NUMBER,参数2 IN NUMBER) AS 变量1 INTEGER := ...

  8. sqlserver 删除表分区

    我们都知道,SQL server2008R2企业版以及一些其它的版本支持分区函数,当你在这些数据库备份后想在一些不支持分区函数的数据库做还原时,就会失败. 下面我们来解决这个问题. 1.备份数据库!备 ...

  9. springMVC WebApplicationInitializer 替代web.xml 配置Servlet 之原理

    Servlet 3.0之前 ,xml  配置 在过去搭建spring + springMCV ,首先第一步要做的是什么 ,就是要配置web.xml 文件 ,把springMVC 中的Servlet 加 ...

  10. 搭建mybatis开发环境

    1.创建工程 <groupId>com.hope</groupId>     <artifactId>day01_eesy_01mybatis</artifa ...