头文件

#include "json/document.h"
#include "json/prettywriter.h"
#include "json/stringbuffer.h"

这是生成数组的

void test()
{
string info = "{\"id\":\"1111111\",\"cards\":[0,1,2,3,4,5,6,7,8,9],\"jiaoDiZhu\":\"\"}"; rapidjson::Document doc;
doc.Parse<>(info.c_str()); rapidjson::Value &dataArray = doc["cards"]; if (dataArray.IsArray())
{
for (rapidjson::SizeType i = ; i < dataArray.Size(); i++)
{
const rapidjson::Value& object = dataArray[i]; printf("%d\n",object.GetInt());
}
}
}
void test()
{
//read json
string updateInfo = "{\"UpdateInfo\":[{\"url\":\"aaaa.ipa\",\"platform\":\"ios\"}]}"; rapidjson::Document doc;
doc.Parse<>(updateInfo.c_str()); rapidjson::Value &dataArray = doc["UpdateInfo"]; if (dataArray.IsArray())
{
for (int i = ; i < dataArray.Size(); i++)
{
const rapidjson::Value& object = dataArray[i]; string url = object["url"].GetString();
string platform = object["platform"].GetString();
}
} //write json
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); rapidjson::Value array(rapidjson::kArrayType); for (int i = ; i < ; i++)
{
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("id", , allocator);
object.AddMember("name", "test", allocator);
object.AddMember("version", 1.01, allocator);
object.AddMember("vip", true, allocator);

object.SetInt(i);
array.PushBack(object, allocator);
} document.AddMember("title", "PLAYER INFO", allocator);
document.AddMember("players", array, allocator); rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
document.Accept(writer);
auto out = buffer.GetString();
log("out: %s", out);
}

下面是单个的

void readTest()
{
char json[] = "{\"carriorName\":\"日本电信\",\"deviceName\":\"iPhone5,2\",\"osName\":\"android\",\"osVersion\":\"8.1\",\"appId\":\"com.mmcshadow.doudizhu\",\"versionName\":\"1.0\",\"versionCode\":\"1.0\",\"deviceId\":\"11111111\"}";
rapidjson::Document d;
d.Parse<>(json);
printf("%s\n", d["carriorName"].GetString());
printf("%s\n", json);
}
void writeTest()
{
rapidjson::Document document;
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
rapidjson::Value root(rapidjson::kObjectType);
root.AddMember("name", "哥伦布啊", allocator);
root.AddMember("gold",,allocator);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
root.Accept(writer);
std::string reststring = buffer.GetString();
std::cout << reststring << std::endl;
}

rapidjson的read和write的sample的更多相关文章

  1. RapidJSON v1.1.0 发布简介

    时隔 15.6 个月,终于发布了一个新版本 v1.1.0. 新版本除了包含了这些日子收集到的无数的小改进及 bug fixes,也有一些新功能.本文尝试从使用者的角度,简单介绍一下这些功能和沿由. P ...

  2. RapidJSON 代码剖析(二):使用 SSE4.2 优化字符串扫描

    现在的 CPU 都提供了单指令流多数据流(single instruction multiple data, SIMD)指令集.最常见的是用于大量的浮点数计算,但其实也可以用在文字处理方面. 其中,S ...

  3. Linux下UPnP sample分析

        一.UPnP简介   UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术,建立在TCP/IP.HTTP协 ...

  4. cocos2d-x for android配置 & 运行 Sample on Linux OS

    1.从http://www.cocos2d-x.org/download下载稳定版 比如cocos2d-x-2.2 2.解压cocos2d-x-2.2.zip,比如本文将其解压到 /opt 目录下 3 ...

  5. android studio2.2 的Find Sample Code点击没有反应

    1 . 出现的问题描述:           右键点击Find Sample Code后半天没有反应,然后提示 Samples are currently unavailable for :{**** ...

  6. jmeter(四)Sample之http请求

    启动jmeter,建立一个测试计划 这里再次说说怎么安装和启动jmeter吧,昨天下午又被人问到怎样安装和使用,我也是醉了:在我看来,百度能解决百分之八十的问题,特别是基础的问题... 安装:去官网下 ...

  7. RapidJSON 代码剖析(四):优化 Grisu

    我曾经在知乎的一个答案里谈及到 V8 引擎里实现了 Grisu 算法,我先引用该文的内容简单介绍 Grisu.然后,再谈及 RapidJSON 对它做了的几个底层优化. (配图中的<Grisù& ...

  8. RapidJSON 代码剖析(三):Unicode 的编码与解码

    根据 RFC-7159: 8.1 Character Encoding JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32. The defa ...

  9. RapidJSON 代码剖析(一):混合任意类型的堆栈

    大家好,这个专栏会分析 RapidJSON (中文使用手册)中一些有趣的 C++ 代码,希望对读者有所裨益. C++ 语法解说 我们先来看一行代码(document.h): bool StartArr ...

随机推荐

  1. python:小乌龟turtle

    turtle的意思是乌龟,也是python中自带的图形函数,使用turtle的方法也很形象,就好像在画布上有一个小乌龟(在画布上是一个箭头),然后你可以让它动来动去,它经过的地方就被留下了记号. 例如 ...

  2. Quartz教程四:Trigger

    原文链接 | 译文链接 | 翻译:nkcoder 本系列教程由quartz-2.2.x官方文档翻译.整理而来,希望给同样对quartz感兴趣的朋友一些参考和帮助,有任何不当或错误之处,欢迎指正:有兴趣 ...

  3. Yii ExtendedActiveRecord 增强版 ActiveRecord 增加多数据库连接绑定功能

    ExtendedActiveRecord 继承自 CActiveRecord,因此基础功能与 CActiveRecord 无异 为添加对多数据库连接的支持,增加了对 connectionName() ...

  4. Linux crontab使用及注意事项

    渗透测试中,经常会用到crontab来执行计划任务,从而实现后门程序的隐藏,但crontab有几个坑点还是需要记录一下,方便日后使用时及时查询 1.crontab配置格式 # Example of j ...

  5. 【HEVC学习与研究】29、解码第一个Coding Quadtree结构(1)

    ctu tree属性 http://blog.csdn.net/shaqoneal/article/details/26088817

  6. UE4中类自动生成代码解析

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/73189272 作者:car ...

  7. "PEP:8 expected 2 blank lines ,found 1"

    pycharm shows "PEP:8 expected 2 blank lines ,found 1" 用pycharm写python的时候,总会在def function() ...

  8. css工具类封装

    温馨提示:一下css封装,建议按需使用,否则会造成很大的代码冗余,且很多样式会造成不符合预期的效果,建议合理使用 <a href="https://meyerweb.com/eric/ ...

  9. 【剑指offer】数字在排序数组中出现的次数,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 利用二分查找法,查找元素k在排序数组中第一次出现的位置m及最后一次出现的位置n,m-n+1即为元素k再排序数组中出现的次数.       二分查找法在数 ...

  10. 添加dom节点及优化

    创建并添加dom加点如何进行优化? 1.使用文档片(DocumentFragment) 可以理解为"仓库",用来保存将来可能会添加到DOM中的节点: var fragment = ...