rapidjson 使用
来源于这篇博客
https://blog.csdn.net/qq_35703954/article/details/82691030?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163004801416780357227263%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&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 -= gui
CONFIG += 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.0
SOURCES += main.cpp
INCLUDEPATH += /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