热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

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

今天小编就为大家分享一篇关于C++中rapidjson组装map和数组array的代码示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

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

直接上码:

#include 
#include 
// 请自己下载开源的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 
#include 
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
// 注意int和uint64_t
map g_mChildInt;
map g_mChildString;
string formJson(const map &mInt, const map &mString,
     const string &strChild="", const map &mChildInt=g_mChildInt, const map &mChildString=g_mChildString,
     const string &strChild2="", const map &mChildInt2=g_mChildInt, const map &mChildString2=g_mChildString)
{
 Document document;
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType);
  Value key(kStringType); 
  Value value(kStringType); 
 // 当前级别
 for(map::const_iterator it = mInt.begin(); it != mInt.end(); ++it) 
 {
 key.SetString(it->first.c_str(), allocator); 
   root.AddMember(key, it->second, allocator);
 }
 for(map::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::const_iterator it = mChildInt.begin(); it != mChildInt.end(); ++it) 
 {
  key.SetString(it->first.c_str(), allocator); 
   child.AddMember(key, it->second, allocator);
 }
 for(map::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::const_iterator it = mChildInt2.begin(); it != mChildInt2.end(); ++it) 
 {
  key.SetString(it->first.c_str(), allocator); 
   child.AddMember(key, it->second, allocator);
 }
 for(map::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 writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
}
string formJsonWithArray(const map &mInt, const map &mString,
  const string &strChild1, const map &mChildInt, const map &mChildString,
     string &strChild2, vector >&mVecChildInt, vector >&mVecChildString)
{
 Document document;
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType);
  Value key(kStringType); 
  Value value(kStringType); 
 // 当前级别
 for(map::const_iterator it = mInt.begin(); it != mInt.end(); ++it) 
 {
 key.SetString(it->first.c_str(), allocator); 
   root.AddMember(key, it->second, allocator);
 }
 for(map::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::const_iterator it = mChildInt.begin(); it != mChildInt.end(); ++it) 
 {
  key.SetString(it->first.c_str(), allocator); 
   child.AddMember(key, it->second, allocator);
 }
 for(map::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 ::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::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 writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
}
void test1()
{
 map mInt;
 map mString;
 mInt["code"] = 0;
 mString["msg"] = "ok";
 string strChild1 = "xxx";
 map mChildInt1;
 map mChildString1;
 mChildInt1["key"] = 729;
 mChildString1["kk"] = "vv";
 string strChild2 = "yyy";
 map mChildInt2;
 map mChildString2;
 mChildInt2["key"] = 730;
 mChildString2["kkk"] = "vvv";
 string s = formJson(mInt, mString, strChild1, mChildInt1, mChildString1,strChild2, mChildInt2, mChildString2);
 cout < mInt;
 map mString;
 mInt["code"] = 0;
 mString["msg"] = "ok";
 string strChild1 = "xxx";
 map mChildInt;
 map mChildString;
 mChildString["kk"] = "vv";
 mChildInt["key"] = 729;
 string strChild2 = "data";
 vector >mVecChildInt; 
 vector >mVecChildString;
 {
 map mChildInt; 
 map mChildString;
 mChildInt["id"] = 1;
 mChildString["path"] = "pa";
 mChildString["sha"] = "sh";
 mVecChildInt.push_back(mChildInt);
 mVecChildString.push_back(mChildString);
 }
 {
 map mChildInt; 
 map 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 <

结果:

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

总结

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


推荐阅读
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • C++实现经典排序算法
    本文详细介绍了七种经典的排序算法及其性能分析。每种算法的平均、最坏和最好情况的时间复杂度、辅助空间需求以及稳定性都被列出,帮助读者全面了解这些排序方法的特点。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 如何在PHPcms网站中添加广告
    本文详细介绍了在PHPcms网站后台添加广告的方法,涵盖多种常见的广告形式,如百度广告和Google广告,并提供了相关设置的步骤。同时,文章还探讨了优化网站流量的SEO策略。 ... [详细]
  • 本文探讨了 RESTful API 和传统接口之间的关键差异,解释了为什么 RESTful API 在设计和实现上具有独特的优势。 ... [详细]
  • 当iOS设备越狱后,某些插件可能会导致系统崩溃(白苹果)。此时,可以通过进入安全模式来排查并删除有问题的插件。本文将详细介绍如何通过特定按键组合进入不加载MobileSubstrate的安全模式,并提供相关背景知识。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • C++: 实现基于类的四面体体积计算
    本文介绍如何使用C++编程语言,通过定义类和方法来计算由四个三维坐标点构成的四面体体积。文中详细解释了四面体体积的数学公式,并提供了两种不同的实现方式。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 如何优化2060显卡设置以提升《Apex英雄》游戏体验
    《Apex英雄》作为一款热门的战术竞技游戏,吸引了大量玩家。本文将探讨如何通过优化GeForce RTX 2060显卡设置,确保在《Apex英雄》中获得最佳性能和流畅的游戏体验。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文介绍如何通过SQL查询从JDE(JD Edwards)系统中提取所有字典数据,涵盖关键表的关联和字段选择。具体包括F0004和F0005系列表的数据提取方法。 ... [详细]
author-avatar
绅(bian)士(tai)
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有