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

相关文章

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

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

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M