rapidjson组装map和数组array的代码示例

2024-02-06 11:58

本文主要是介绍rapidjson组装map和数组array的代码示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

         直接上码:

 

#include <iostream>
#include <map>// 请自己下载开源的rapidjson
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
#include <sys/types.h>
#include <vector>using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;// 注意int和uint64_t
map<string, uint64_t> g_mChildInt;
map<string, string> g_mChildString;
string formJson(const map<string, int> &mInt, const map<string, string> &mString,const string &strChild="", const map<string, uint64_t> &mChildInt=g_mChildInt, const map<string, string> &mChildString=g_mChildString,const string &strChild2="", const map<string, uint64_t> &mChildInt2=g_mChildInt, const map<string, string> &mChildString2=g_mChildString)
{Document document;Document::AllocatorType& allocator = document.GetAllocator(); Value root(kObjectType);Value key(kStringType);  Value value(kStringType); // 当前级别for(map<string, int>::const_iterator it = mInt.begin(); it != mInt.end(); ++it) {key.SetString(it->first.c_str(), allocator);  root.AddMember(key, it->second, allocator);}for(map<string, string>::const_iterator it = mString.begin(); it != mString.end(); ++it){key.SetString(it->first.c_str(), allocator);  value.SetString(it->second.c_str(), allocator);  root.AddMember(key, value, allocator);}// 孩子级别if(!strChild.empty()){Value child(kObjectType);for(map<string, uint64_t>::const_iterator it = mChildInt.begin(); it != mChildInt.end(); ++it) {key.SetString(it->first.c_str(), allocator);  child.AddMember(key, it->second, allocator);}for(map<string, string>::const_iterator it = mChildString.begin(); it != mChildString.end(); ++it){key.SetString(it->first.c_str(), allocator);  value.SetString(it->second.c_str(), allocator);  child.AddMember(key, value, allocator);}key.SetString(strChild.c_str(), allocator); root.AddMember(key, child, allocator);}// 孩子级别if(!strChild2.empty()){Value child(kObjectType);for(map<string, uint64_t>::const_iterator it = mChildInt2.begin(); it != mChildInt2.end(); ++it) {key.SetString(it->first.c_str(), allocator);  child.AddMember(key, it->second, allocator);}for(map<string, string>::const_iterator it = mChildString2.begin(); it != mChildString2.end(); ++it){key.SetString(it->first.c_str(), allocator);  value.SetString(it->second.c_str(), allocator);  child.AddMember(key, value, allocator);}key.SetString(strChild2.c_str(), allocator); root.AddMember(key, child, allocator);}StringBuffer buffer;  Writer<StringBuffer> writer(buffer);  root.Accept(writer);  return buffer.GetString();  }string formJsonWithArray(const map<string, int> &mInt, const map<string, string> &mString,const string &strChild1, const map<string, uint64_t> &mChildInt, const map<string, string> &mChildString,string &strChild2, vector<map<string, uint64_t> >&mVecChildInt, vector<map<string, string> >&mVecChildString){Document document;Document::AllocatorType& allocator = document.GetAllocator(); Value root(kObjectType);Value key(kStringType);  Value value(kStringType); // 当前级别for(map<string, int>::const_iterator it = mInt.begin(); it != mInt.end(); ++it) {key.SetString(it->first.c_str(), allocator);  root.AddMember(key, it->second, allocator);}for(map<string, string>::const_iterator it = mString.begin(); it != mString.end(); ++it){key.SetString(it->first.c_str(), allocator);  value.SetString(it->second.c_str(), allocator);  root.AddMember(key, value, allocator);}// 孩子级别if(!strChild1.empty()){Value child(kObjectType);for(map<string, uint64_t>::const_iterator it = mChildInt.begin(); it != mChildInt.end(); ++it) {key.SetString(it->first.c_str(), allocator);  child.AddMember(key, it->second, allocator);}for(map<string, string>::const_iterator it = mChildString.begin(); it != mChildString.end(); ++it){key.SetString(it->first.c_str(), allocator);  value.SetString(it->second.c_str(), allocator);  child.AddMember(key, value, allocator);}key.SetString(strChild1.c_str(), allocator); root.AddMember(key, child, allocator);}// 孩子级别unsigned int uiSize1 = mVecChildInt.size();unsigned int uiSize2 = mVecChildString.size();if(!strChild2.empty() && uiSize1 == uiSize2){Value array(rapidjson::kArrayType);  for(unsigned int i = 0; i < uiSize1; ++i){Value child(kObjectType);for(map<string, uint64_t>::iterator it = mVecChildInt[i].begin(); it != mVecChildInt[i].end(); ++it) {key.SetString(it->first.c_str(), allocator);  child.AddMember(key, it->second, allocator);}for(map<string, string>::iterator it = mVecChildString[i].begin(); it != mVecChildString[i].end(); ++it){key.SetString(it->first.c_str(), allocator);  value.SetString(it->second.c_str(), allocator);  child.AddMember(key, value, allocator);}array.PushBack(child, allocator);  }key.SetString(strChild2.c_str(), allocator); root.AddMember(key, array, allocator);}StringBuffer buffer;  Writer<StringBuffer> writer(buffer);  root.Accept(writer);  return buffer.GetString();  
}void test1()
{map<string, int> mInt;map<string, string> mString;mInt["code"] = 0;mString["msg"] = "ok";string strChild1 = "xxx";map<string, uint64_t> mChildInt1;map<string, string> mChildString1;mChildInt1["key"] = 729;mChildString1["kk"] = "vv";string strChild2 = "yyy";map<string, uint64_t> mChildInt2;map<string, string> mChildString2;mChildInt2["key"] = 730;mChildString2["kkk"] = "vvv";string s = formJson(mInt, mString, strChild1, mChildInt1, mChildString1,strChild2, mChildInt2, mChildString2);cout << s << endl;
}void test2()
{map<string, int> mInt;map<string, string> mString;mInt["code"] = 0;mString["msg"] = "ok";string strChild1 = "xxx";map<string, uint64_t> mChildInt;map<string, string> mChildString;mChildString["kk"] = "vv";mChildInt["key"] = 729;string strChild2 = "data";vector<map<string, uint64_t> >mVecChildInt; vector<map<string, string> >mVecChildString;{map<string, uint64_t> mChildInt; map<string, string> mChildString;mChildInt["id"] = 1;mChildString["path"] = "pa";mChildString["sha"] = "sh";mVecChildInt.push_back(mChildInt);mVecChildString.push_back(mChildString);}{map<string, uint64_t> mChildInt; map<string, string> mChildString;mChildInt["id"] = 2;mChildString["path"] = "pa";mChildString["sha"] = "sh";mVecChildInt.push_back(mChildInt);mVecChildString.push_back(mChildString);}string s = formJsonWithArray(mInt, mString, strChild1, mChildInt, mChildString, strChild2, mVecChildInt, mVecChildString);cout << s << endl;
}int main(int argc, char *argv[])
{test1();test2();return 0;
}

         结果:

 

{"code":0,"msg":"ok","xxx":{"key":729,"kk":"vv"},"yyy":{"key":730,"kkk":"vvv"}}
{"code":0,"msg":"ok","xxx":{"key":729,"kk":"vv"},"data":[{"id":1,"path":"pa","sha":"sh"},{"id":2,"path":"pa","sha":"sh"}]}
 

 

        不多说, 睡觉。

 

这篇关于rapidjson组装map和数组array的代码示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/684241

相关文章

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

前端高级CSS用法示例详解

《前端高级CSS用法示例详解》在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交互和动态效果的关键技术之一,随着前端技术的不断发展,CSS的用法也日益丰富和高级,本文将深... 前端高级css用法在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu