//包含头文件
#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);
if(arrsub.IsNull())
{
std::cout<<"arrsub转移了"<<std::endl;
rapidjson实例
最新推荐文章于 2026-03-24 00:47:46 发布
这篇博客介绍了如何使用rapidjson库解析和输出JSON字符串,包括解析后的数据结构展示以及浅拷贝和深拷贝的概念,并给出了具体的输出结果示例。


2259

被折叠的 条评论
为什么被折叠?



