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

2024-02-06 11:58

本文主要是介绍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函数, 还可以继续扩展。

 

 

这篇关于rapidjson将嵌套map转为嵌套json------人生苦短,我用rapidjson的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java Map排序如何按照值按照键排序

《JavaMap排序如何按照值按照键排序》该文章主要介绍Java中三种Map(HashMap、LinkedHashMap、TreeMap)的默认排序行为及实现按键排序和按值排序的方法,每种方法结合实... 目录一、先理清 3 种 Map 的默认排序行为二、按「键」排序的实现方式1. 方式 1:用 TreeM

java中判断json key是否存在的几种方法

《java中判断jsonkey是否存在的几种方法》在使用Java处理JSON数据时,如何判断某一个key是否存在?本文就来介绍三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目http://www.chinasem.cn录第一种方法是使用 jsONObject 的 has 方法

Go语言中json操作的实现

《Go语言中json操作的实现》本文主要介绍了Go语言中的json操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、jsOChina编程N 与 Go 类型对应关系️ 二、基本操作:编码与解码 三、结构体标签(Struc

利用Python把路径转为绝对路径的方法

《利用Python把路径转为绝对路径的方法》在Python中,如果你有一个相对路径并且想将其转换为绝对路径,你可以使用Path对象的resolve()方法,Path是Python标准库pathlib中... 目录1. os.path.abspath 是什么?怎么用?基本用法2. os.path.abspat

java时区时间转为UTC的代码示例和详细解释

《java时区时间转为UTC的代码示例和详细解释》作为一名经验丰富的开发者,我经常被问到如何将Java中的时间转换为UTC时间,:本文主要介绍java时区时间转为UTC的代码示例和详细解释,文中通... 目录前言步骤一:导入必要的Java包步骤二:获取指定时区的时间步骤三:将指定时区的时间转换为UTC时间步

Vue3 如何通过json配置生成查询表单

《Vue3如何通过json配置生成查询表单》本文给大家介绍Vue3如何通过json配置生成查询表单,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录功能实现背景项目代码案例功能实现背景通过vue3实现后台管理项目一定含有表格功能,通常离不开表单

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映