rapidjson 使用
来源于这篇博客https://blog.csdn.net/qq_35703954/article/details/82691030?ops_request_misc=%7B%22request%5Fid%22%3A%22163004801416780357227263%22%2C%22scm%22%3A%2220140713.130102334.pc%5Fblog.%22%7D&request_id=163004801416780357227263&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_v2~rank_v29-1-82691030.pc_v2_rank_blog_default&utm_term=json&spm=1018.2226.3001.4450
rapidjson生成复杂的json串
{"name":"jack",//常规的"age":18,"sub":["a","b"],//value是数组"elp":[ {"a":"A","b":"B"},//value是一个数组且里面每个元素又是一个json格式{"c":"C","d":"D"},]
}
//#include "rapidjson/stringbuffer.h"
//#include "rapidjson/writer.h"#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/stringbuffer.h>#include <iostream>std::string build_json_msg(){rapidjson::Document doc;//生成DOM元素doc.SetObject();rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();//生成一个分配器//构建键值对doc.AddMember("name","jack",allocator);doc.AddMember("age",18,allocator);//====构建数组元素====["1","2"]rapidjson::Value array_sub(rapidjson::kArrayType);//创建一个数组类型对象array_sub.PushBack("a",allocator);array_sub.PushBack("b",allocator);//================doc.AddMember("sub",array_sub,allocator);//====构建数组object===[{"1":2,},{}]rapidjson::Value array_json(rapidjson::kArrayType);rapidjson::Value obj(rapidjson::kObjectType);//每一个数组里面是一个json格式obj.AddMember("a","A",allocator);obj.AddMember("b","B",allocator);array_json.PushBack(obj,allocator);rapidjson::Value obj1(rapidjson::kObjectType);//每一个数组里面是一个json格式obj1.AddMember("c","C",allocator);obj1.AddMember("d","D",allocator);array_json.PushBack(obj1,allocator);
// array_json.PushBack(obj1,allocator);doc.AddMember("elp",array_json,allocator);rapidjson::StringBuffer s;rapidjson::Writer<rapidjson::StringBuffer> writer(s);doc.Accept(writer);return std::string(s.GetString());}int main()
{std::string str_json = build_json_msg();std::cout<<str_json<<std::endl;std::cout<<"hello world"<<std::endl;return 0;
}
我用的qt,其中.pro如下:
QT -= guiCONFIG += c++11 console
CONFIG -= app_bundle# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += main.cppINCLUDEPATH += /data_2/project_202009/project/3rdparty/
程序跑起来打印如下:
{"name":"jack","age":18,"sub":["a","b"],"elp":[{"a":"A","b":"B"},{"c":"C","d":"D"}]}
hello world
按 <RETURN> 来关闭窗口...
在网站json.cn可以格式化显示json:
{"name":"jack","age":18,"sub":["a","b"],"elp":[{"a":"A","b":"B"},{"c":"C","d":"D"}]
}
在生成的时候我们需要几个十分重要的对象,Document、AllocatorType。刚开始的三行代码是比较固定的,先申明一个doc和一个分配器。
rapidjson::Document doc;//生成DOM元素
doc.SetObject();
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();//生成一个分配器
我们添加json元素主要通过doc.AddMember(key,value,分配器)的形式来生成。如果json格式很简单,那么代码也很简单,如下:
doc.AddMember("name","jack",allocator);
doc.AddMember("age",18,allocator);
但是,如果涉及到数组。就需要注意一下。首先,我们需要创建一个数组类型的对象。然后把数组里面的内容PushBack进去。最后同样通过doc.AddMember()添加进去。但是value却变成了array_sub.说明AddMember()的第二个参数,不仅可以是值还可以是对象。
rapidjson::Value array_sub(rapidjson::kArrayType);//创建一个数组类型对象array_sub.PushBack("a",allocator);array_sub.PushBack("b",allocator);//================
doc.AddMember("sub",array_sub,allocator);
同样,如果数组里面是json串,该怎么办呢?其实写法按着上面的逻辑来,看上面的代码,只不过把‘a’换成object类型的。具体写法如下:
//====构建数组object===[{"1":2,},{}]rapidjson::Value array_json(rapidjson::kArrayType);rapidjson::Value obj(rapidjson::kObjectType);//每一个数组里面是一个json格式obj.AddMember("a","A",allocator);obj.AddMember("b","B",allocator);array_json.PushBack(obj,allocator);rapidjson::Value obj1(rapidjson::kObjectType);//每一个数组里面是一个json格式obj1.AddMember("c","C",allocator);obj1.AddMember("d","D",allocator);array_json.PushBack(obj1,allocator);array_json.PushBack(obj1,allocator);doc.AddMember("elp",array_json,allocator);
读写json参考链接:https://www.cnblogs.com/fnlingnzb-learner/p/10334988.html
rapidjson 使用相关推荐
- RapidJSON简介及使用
RapidJSON是腾讯开源的一个高效的C++ JSON解析器及生成器,它是只有头文件的C++库.RapidJSON是跨平台的,支持Windows, Linux, Mac OS X及iOS, Andr ...
- rapidJson输出时 保留小数位
pretty_writer.SetMaxDecimalPlaces(4); 这个真好用,它使用gresu, 尽可能给你处理最接近的精度,并按要求输出小数位. #include <string&g ...
- 推荐一款cpp解析json工具--rapidjson
项目地址:http://code.google.com/p/rapidjson/ 上面有很详细的介绍:http://code.google.com/p/rapidjson/wiki/UserGuide ...
- rapidjson的read和write的sample
#include "json/document.h" #include "json/prettywriter.h" #include "json/st ...
- rapidjson官方教程
原文地址:http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html 教程 目录 Value 及 Document 查询Value 查询Array 查 ...
- jsoncpp和rapidjson哪个好用?
作者:Milo Yip 链接:http://www.zhihu.com/question/23654513/answer/46157616 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非 ...
- rapidjson 嵌套json_[整理][RapidJson用法2]C++之RapidJson创建嵌套数组json | 勤奋的小青蛙...
上篇博文里我通过RapidJson解析了嵌套的json文本,那么本文章是RapidJson创建嵌套数组Json文本. 代码: #include "rapidjson/document.h&q ...
- C++学习笔记-利用rapidJSON读取JSON数据
JSON文件如下: {"errorCode":0,"reason":"OK","result":{"userI ...
- C++学习笔记-利用rapidJSON生成JSON数据
RapidJSON is a header-only C++ library 在json.org中下载rapidjson 无需加dll和lib,只要加上头文件即可! 结构图如下: 源码如下: #inc ...
- RapidJson 的使用
rapidjson为了最大化性能,大量使用了浅拷贝,使用之前一定要了解清楚.如果采用了浅拷贝,特别要注意局部对象的使用,以防止对象已被析构了,却还在被使用. rapidjson使用注意点: 1.对不存 ...
最新文章
- 专访趋势科技CEO陈怡桦:病毒行业需要反省
- Cisco 3560 丢失 IOS 解决过程
- Asp.net mvc中的Ajax处理
- 稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 绑定数据到ListBox
- 【人物】Paul Graham: 卑鄙的人一定失败
- python控制树莓派led_Python 控制树莓派 GPIO 输出:控制 LED 灯
- innobackupex中--slave-info参数的含义和适用场景
- 每天30分钟:成功有效的学习方法
- 数据结构之图:有向图的拓扑排序,Python代码实现——26
- 集合框架(Map容器/Collections工具类)
- mysql技术简介_MySQL数据类型介绍
- db2 设置表 not null
- VirtualBox配置
- matlab 关于double和im2double
- ARCGIS中生成KML文件
- Linux socket等于0,Linux系统环境下的Socket编程详细解析
- 算法:Invert Binary Tree(翻转二叉树)
- magisk下载里显示没有模块_magisk框架
- 一个让人不得不转的故事-《通宵达旦工资只有3200 博客网架构师艰难浪迹于北京》...
- n的约数(数论+DFS)
热门文章
- 安装dd-wrt_如何在家庭路由器(DD-WRT)上安装其他软件
- 世界那么大,谁不想去看看
- 《羊卓的杨的算法笔记》_Quentin
- Fortify的各种疑难杂症
- 十进制点分IP转换为32位二进制IP(C++)
- SphereFace2 Binary Classification is All You Need for Deep Face Recognition
- Android Studio NDK 开发
- 数据库查询结果ResultSet详解
- 以服务为核心推出线上回收计划
- 贪心策略(一)(选择排序、分割平衡串、买卖股票、跳跃游戏)