C++中rapidjson将嵌套map转为嵌套json的讲解

 更新时间:2019年04月08日 11:01:43   作者:stpeace  
今天小编就为大家分享一篇关于C++中rapidjson将嵌套map转为嵌套json的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

rapidjson将嵌套map转为嵌套json------人生苦短,我用rapidjson

看代码:

#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"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
string formJson(const map<string, int> &mInt, const map<string, string> &mString,
     const string &strChild, const map<string, int> &mChildInt, const map<string, string> &mChildString)
{
 Document document;
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType);
  Value child(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())
 {
 for(map<string, int>::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);
 }
  StringBuffer buffer; 
  Writer<StringBuffer> writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
}
int main(int argc, char *argv[])
{
 map<string, int> mInt;
 mInt["code"] = 0;
 mInt["score"] = 80;
 map<string, string> mString;
 mString["name"] = "taoge";
 mString["place"] = "shenzhen";
 string strChild = "childNode";
 map<string, int> mChildInt;
 mChildInt["code"] = 0;
 mChildInt["score"] = 100;
 map<string, string> mChildString;
 mChildString["name"] = "taogeChild";
 mChildString["place"] = "shenzhen";
 string strJson = formJson(mInt, mString, 
            strChild, mChildInt, mChildString);
 cout << strJson << endl;
 return 0;
}

结果:

{"code":0,"score":80,"name":"taoge","place":"shenzhen","childNode":{"code":0,"score":100,"name":"taogeChild","place":"shenzhen"}}

另外, 如果仅仅想有当前界别, 那么, 可以这么搞(C++默认参数搞起):

#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"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
map<string, int> 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, int> &mChildInt=g_mChildInt, const map<string, string> &mChildString=g_mChildString)
{
 Document document;
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType);
  Value child(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())
 {
 for(map<string, int>::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);
 }
  StringBuffer buffer; 
  Writer<StringBuffer> writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
}
int main(int argc, char *argv[])
{
 map<string, int> mInt;
 mInt["code"] = 0;
 mInt["score"] = 80;
 map<string, string> mString;
 mString["name"] = "taoge";
 mString["place"] = "shenzhen";
 string strJson = formJson(mInt, mString);
 cout << strJson << endl;
 return 0;
}

结果:

{"code":0,"score":80,"name":"taoge","place":"shenzhen"}

其实, 上面的formJson函数, 还可以继续扩展。

总结

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

相关文章

  • C语言实现动态链表的示例代码

    C语言实现动态链表的示例代码

    本文主要介绍了C语言实现动态链表的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • c++中关于int、long、long long等取值范围

    c++中关于int、long、long long等取值范围

    这篇文章主要介绍了c++中关于int、long、long long等取值范围,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • C语言连接并操作Sedna XML数据库的方法

    C语言连接并操作Sedna XML数据库的方法

    这篇文章主要介绍了C语言连接并操作Sedna XML数据库的方法,实例分析了C语言操作XML文件的相关技巧,需要的朋友可以参考下
    2015-06-06
  • 使用C++的ORM框架QxORM详解

    使用C++的ORM框架QxORM详解

    这篇文章主要介绍了使用C++的ORM框架QxORM的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C++如何过滤出字符串的中文(GBK、UTF-8)

    C++如何过滤出字符串的中文(GBK、UTF-8)

    这篇文章主要给大家介绍了关于C++如何过滤出字符串的中文的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C++具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • C++服务器和客户端交互的项目实践

    C++服务器和客户端交互的项目实践

    本文主要介绍了C++服务器和客户端交互的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • C++中Cbitmap,HBitmap,Bitmap区别及联系

    C++中Cbitmap,HBitmap,Bitmap区别及联系

    这篇文章主要介绍了C++中Cbitmap,HBitmap,Bitmap区别及联系的相关资料,需要的朋友可以参考下
    2015-06-06
  • C++利用数组(一维/二维)处理批量数据的方法

    C++利用数组(一维/二维)处理批量数据的方法

    对于简单的问题,使用简单的数据类型就可以了,但是对于有些需要处理的数据,只用以上简单的数据类型是不够的,难以反映出数据的特点,也难以有效的进行处理,本文小编给大家介绍了C++利用数组(一维/二维)处理批量数据的方法,需要的朋友可以参考下
    2023-10-10
  • C++实现LeetCode(769.可排序的最大块数)

    C++实现LeetCode(769.可排序的最大块数)

    这篇文章主要介绍了C++实现LeetCode(769.可排序的最大块数),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++中浅拷贝与深拷贝的详解及其作用介绍

    C++中浅拷贝与深拷贝的详解及其作用介绍

    这篇文章主要介绍了C++中浅拷贝与深拷贝的详解及其作用介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09

最新评论