//包含头文件
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
//生成json字符串
int main()
{
rapidjson::Document doc; //获取Document对象
doc.SetObject(); //key-value 相当于map; doc.SetArray(); //数组型 相当于vector
rapidjson::Document::AllocatorType &allocator=doc.GetAllocator(); //获取分配器
doc.AddMember("name","名字",allocator);//给doc对象赋值
//添加数组数据
rapidjson::Value array(rapidjson::kArrayType);
for(int i=0;i<3;i++)
{
rapidjson::Value arrsub(rapidjson::kArrayType);
arrsub.PushBack(i, allocator);
//arrsub.PushBack(rapidjson::Value("aa", allocator).Move(), allocator);
arrsub.PushBack(rapidjson::Value("aa", std::strlen("aa"), allocator).Move(), allocator);
arrsub.PushBack(i+1.0, allocator);
array.PushBack(arrsub,allocator);
rapidjson实例
最新推荐文章于 2024-06-06 14:52:25 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)