1.官方文档

http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html#CreateString

2.添加元素

#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/document.h"void rapid_json_test() {rapidjson::Document document;rapidjson::Document::AllocatorType& alloc = document.GetAllocator();rapidjson::Value json_normal(rapidjson::Type::kObjectType);// Object中加key:"value",3中不同key,value的添加形式json_normal.AddMember("key_name", "111", alloc);std::string str_tmp("value1_" + std::to_string(1));json_normal.AddMember("key2_2",rapidjson::Value().SetString(str_tmp.c_str(), str_tmp.length(), alloc).Move(), alloc);std::string str_key3("key3_" + std::to_string(3));std::string str_val3("value3_" + std::to_string(3));json_normal.AddMember(rapidjson::Value(str_key3.c_str(), str_key3.length(), alloc).Move(),rapidjson::Value(str_val3.c_str(), str_val3.length(), alloc).Move(),alloc);// Arrayrapidjson::Value json_array(rapidjson::Type::kArrayType);// Araay中添加元素json_array.PushBack(json_normal.Move(), alloc);  // Objectstd::string err_str("error,key:" + std::string("keyx"));json_array.PushBack(rapidjson::Value(err_str.c_str(), err_str.length(), alloc), alloc);  // String// 转Stringrapidjson::StringBuffer buffer;rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);json_array.Accept(writer);std::cout << "final string:\n" << buffer.GetString() << "\n";
}

效果:

[{"key_name":"111","key2_2":"value1_1","key3_3":"value3_3"},"error,key:keyx"]

3.解析Json

const char* response_data = "{\"key\":123}";
rapidjson::Document document;
document.Parse<0>(response_data);if (document.HasMember("key") && ducument["key"].IsInt()) {int value = ducument["key"].GetInt();
}

注:

1.Array类型的Json中是不允许出现 key:"value"的形式的,如果强行AddMember, 那么只会显示key

2.Object中要求每一项必须有key

rapidJson使用相关推荐

  1. RapidJSON简介及使用

    RapidJSON是腾讯开源的一个高效的C++ JSON解析器及生成器,它是只有头文件的C++库.RapidJSON是跨平台的,支持Windows, Linux, Mac OS X及iOS, Andr ...

  2. rapidJson输出时 保留小数位

    pretty_writer.SetMaxDecimalPlaces(4); 这个真好用,它使用gresu, 尽可能给你处理最接近的精度,并按要求输出小数位. #include <string&g ...

  3. 推荐一款cpp解析json工具--rapidjson

    项目地址:http://code.google.com/p/rapidjson/ 上面有很详细的介绍:http://code.google.com/p/rapidjson/wiki/UserGuide ...

  4. rapidjson的read和write的sample

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

  5. rapidjson官方教程

    原文地址:http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html 教程 目录 Value 及 Document 查询Value 查询Array 查 ...

  6. jsoncpp和rapidjson哪个好用?

    作者:Milo Yip 链接:http://www.zhihu.com/question/23654513/answer/46157616 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非 ...

  7. rapidjson 嵌套json_[整理][RapidJson用法2]C++之RapidJson创建嵌套数组json | 勤奋的小青蛙...

    上篇博文里我通过RapidJson解析了嵌套的json文本,那么本文章是RapidJson创建嵌套数组Json文本. 代码: #include "rapidjson/document.h&q ...

  8. C++学习笔记-利用rapidJSON读取JSON数据

    JSON文件如下: {"errorCode":0,"reason":"OK","result":{"userI ...

  9. C++学习笔记-利用rapidJSON生成JSON数据

    RapidJSON is a header-only C++ library 在json.org中下载rapidjson 无需加dll和lib,只要加上头文件即可! 结构图如下: 源码如下: #inc ...

  10. RapidJson 的使用

    rapidjson为了最大化性能,大量使用了浅拷贝,使用之前一定要了解清楚.如果采用了浅拷贝,特别要注意局部对象的使用,以防止对象已被析构了,却还在被使用. rapidjson使用注意点: 1.对不存 ...

最新文章

  1. java 页面错误转发提示页面 errorPage转跳报HTTP500内部服务器错误
  2. Linux下多线程模拟停车场停车
  3. c语言 自动化编译环境,《C编程.开始C》3.编译基础
  4. 高新计算机office2010考试题库,注意!注意!计算机等级考试题库来啦:一级MS Office第三章“Word2010基础”...
  5. ios下fixed回复框bug的解决方案
  6. Java中四大代码块的执行顺序(附code)
  7. dll反编译(dll反编译工具)
  8. c语言et1什么意思,et cetera是什么意思.如何正确使用 “etc.”
  9. 加菲猫经典语录(中英文对照)
  10. Vue element 日期获取展示今天日期
  11. 基恩士pda开发指南
  12. 计算机组成原理位移量,计算机组成原理大题解析.doc
  13. css引入本地字体文件,关于css中引入字体文件
  14. 一些网站...........
  15. ie的主页被修改了,改不过来怎么办??
  16. [CF1131F] Asya And Kittens
  17. win10无法连接校园网问题
  18. 基于html+JavaScript+css的飞机射击小游戏网页设计与实现
  19. 字体大宝库:20款超细英文字体免费下载
  20. C语言——逆序乘积式

热门文章

  1. 交付管理——怎样提升团队战斗力
  2. MySQL数据库学习资料(十)
  3. 删除数组中的某个元素-JAVA
  4. 利用python爬虫电影分析_python 爬虫分析30年香港电影
  5. CC2530设置发射功率
  6. 【2021-03-20】【Mybatis】Mybatis 判断字符串非空和空串 报错, Encountered “ <IDENT> “AND ““ at line 1
  7. Kubernetes 1.20:最优秀、美妙、酷的版本
  8. Android:长度单位详解(px、dp、sp)
  9. 跟着鬼哥学爬虫-5-更新妹子图库质量,提高微信群聊发图速度
  10. 【天堂电影院】-人生的缩影