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

 更新时间:2019年04月08日 10:34:13   作者:stpeace  
今天小编就为大家分享一篇关于C++中rapidjson组装map和数组array的代码示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

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"}]}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • 详解C++调用Python脚本中的函数的实例代码

    详解C++调用Python脚本中的函数的实例代码

    这篇文章主要介绍了C++调用Python脚本中的函数 ,需要的朋友可以参考下
    2018-11-11
  • c++优先队列(priority_queue)用法详解

    c++优先队列(priority_queue)用法详解

    这篇文章主要介绍了c++优先队列(priority_queue)用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • C语言实现简单弹跳小球

    C语言实现简单弹跳小球

    这篇文章主要为大家详细介绍了C语言实现简单弹跳小球,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • C语言数据结构之 折半查找实例详解

    C语言数据结构之 折半查找实例详解

    这篇文章主要介绍了C语言数据结构之 折半查找实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • C语言中查询进程信号是否被遮罩或搁置的简单方法

    C语言中查询进程信号是否被遮罩或搁置的简单方法

    这篇文章主要介绍了C语言中查询进程信号是否被遮罩或搁置的简单方法,包括sigprocmask函数和sigpending函数的简介,需要的朋友可以参考下
    2015-09-09
  • 实战开发为单片机的按键加一个锁防止多次触发的细节

    实战开发为单片机的按键加一个锁防止多次触发的细节

    今天小编就为大家分享一篇关于实战开发为单片机的按键加一个锁防止多次触发的细节,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • C++实现分数计算器

    C++实现分数计算器

    这篇文章主要为大家详细介绍了C++实现分数计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • C++可变参数的实现方法

    C++可变参数的实现方法

    可变参数给编程带来了很大的方便,在享受它带来的方便的同时,很有必要了解一下其实现方式,在了解编程语言的同时,也可以扩展编程的思路。
    2013-03-03
  • C++深入讲解new与deleted关键字的使用

    C++深入讲解new与deleted关键字的使用

    这篇文章主要介绍了C++中new与deleted关键字的使用,new在动态内存中为对象分配空间并返回一个指向该对象的指针;delete接受一个动态对象的指针, 销毁该对象, 并释放与之关联的内存
    2022-05-05
  • C语言超详细讲解双向带头循环链表

    C语言超详细讲解双向带头循环链表

    带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单
    2023-02-02

最新评论