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中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT