服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++中rapidjson组装map和数组array的代码示例

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

2021-07-27 12:15stpeace C/C++

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

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

直接上码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#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"
#include <sys/types.h>
#include <vector>
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
// 注意int和uint64_t
map<string, uint64_t> 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, uint64_t> &mChildInt=g_mChildInt, const map<string, string> &mChildString=g_mChildString,
     const string &strChild2="", const map<string, uint64_t> &mChildInt2=g_mChildInt, const map<string, string> &mChildString2=g_mChildString)
{
 Document document;
  Document::AllocatorType& allocator = document.GetAllocator();
  Value root(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())
 {
 Value child(kObjectType);
 for(map<string, uint64_t>::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);
 }
 // 孩子级别
 if(!strChild2.empty())
 {
 Value child(kObjectType);
 for(map<string, uint64_t>::const_iterator it = mChildInt2.begin(); it != mChildInt2.end(); ++it)
 {
  key.SetString(it->first.c_str(), allocator);
   child.AddMember(key, it->second, allocator);
 }
 for(map<string, string>::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<StringBuffer> writer(buffer);
  root.Accept(writer);
  return buffer.GetString();
}
string formJsonWithArray(const map<string, int> &mInt, const map<string, string> &mString,
  const string &strChild1, const map<string, uint64_t> &mChildInt, const map<string, string> &mChildString,
     string &strChild2, vector<map<string, uint64_t> >&mVecChildInt, vector<map<string, string> >&mVecChildString)
{
 Document document;
  Document::AllocatorType& allocator = document.GetAllocator();
  Value root(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(!strChild1.empty())
 {
 Value child(kObjectType);
 for(map<string, uint64_t>::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(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 < uiSize1; ++i)
 {
  Value child(kObjectType);
  for(map<string, uint64_t>::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<string, string>::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<StringBuffer> writer(buffer);
  root.Accept(writer);
  return buffer.GetString();
}
void test1()
{
 map<string, int> mInt;
 map<string, string> mString;
 mInt["code"] = 0;
 mString["msg"] = "ok";
 string strChild1 = "xxx";
 map<string, uint64_t> mChildInt1;
 map<string, string> mChildString1;
 mChildInt1["key"] = 729;
 mChildString1["kk"] = "vv";
 string strChild2 = "yyy";
 map<string, uint64_t> mChildInt2;
 map<string, string> mChildString2;
 mChildInt2["key"] = 730;
 mChildString2["kkk"] = "vvv";
 string s = formJson(mInt, mString, strChild1, mChildInt1, mChildString1,strChild2, mChildInt2, mChildString2);
 cout << s << endl;
}
void test2()
{
 map<string, int> mInt;
 map<string, string> mString;
 mInt["code"] = 0;
 mString["msg"] = "ok";
 string strChild1 = "xxx";
 map<string, uint64_t> mChildInt;
 map<string, string> mChildString;
 mChildString["kk"] = "vv";
 mChildInt["key"] = 729;
 string strChild2 = "data";
 vector<map<string, uint64_t> >mVecChildInt;
 vector<map<string, string> >mVecChildString;
 {
 map<string, uint64_t> mChildInt;
 map<string, string> mChildString;
 mChildInt["id"] = 1;
 mChildString["path"] = "pa";
 mChildString["sha"] = "sh";
 mVecChildInt.push_back(mChildInt);
 mVecChildString.push_back(mChildString);
 }
 {
 map<string, uint64_t> mChildInt;
 map<string, string> 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 << s << endl;
}
int main(int argc, char *argv[])
{
 test1();
 test2();
 return 0;
}

结果:

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

总结

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

延伸 · 阅读

精彩推荐
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22