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

相关文章

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

《详解MySQL中JSON数据类型用法及与传统JSON字符串对比》MySQL从5.7版本开始引入了JSON数据类型,专门用于存储JSON格式的数据,本文将为大家简单介绍一下MySQL中JSON数据类型... 目录前言基本用法jsON数据类型 vs 传统JSON字符串1. 存储方式2. 查询方式对比3. 索引

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.