rapidjson组装map和数组array的代码示例
直接上码:
#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编码很陌生,但我正在学习这门课程。 我想写一段代码,找出数组中r个元素的组合,然后对结果进行置换。 我已经能够从各种来源进行研究,并有单独的代码,将打印组合和排列的数组。 我面临的挑战是如何将两个代码结合起来并使其作为一个整体工作。 第一个代码用于 5 个元素数组的组合,其中一次选择 4 个元素。 第二个代码是排列数组组合(前一个代码)的每个结果,这意味着4个元素的排列。在下面的这段
本文向大家介绍理解Golang中的数组(array)、切片(slice)和map,包括了理解Golang中的数组(array)、切片(slice)和map的使用技巧和注意事项,需要的朋友参考一下 我比较喜欢先给出代码,然后得出结论 数组 结果 切片 结果 map 运行结果 由此,我们看到数组本身传过去的是值,传到函数之后,被开辟了另外一个空间。 因为数组就是他本身。这一句好像不太好理解。 这是切片
本文向大家介绍C++中rapidjson将map转为json的方法,包括了C++中rapidjson将map转为json的方法的使用技巧和注意事项,需要的朋友参考一下 rapidjson将map转为json------人生苦短,我用rapidjson 直接撸代码: 结果: {"name":"taoge","place":"shenzhen"} 来,继续改: 结果: {"age":29,"score
NumPy包中包含一个迭代器对象numpy.nditer 。 它是一个有效的多维迭代器对象,使用它可以迭代数组。 使用Python的标准Iterator接口访问数组的每个元素。 让我们使用nditer ()函数创建一个3X4数组,并使用nditer迭代它。 例子1 (Example 1) import numpy as np a = np.arange(0,60,5) a = a.reshape(
一、定长数组 在 Scala 中,如果你需要一个长度不变的数组,可以使用 Array。但需要注意以下两点: 在 Scala 中使用 (index) 而不是 [index] 来访问数组中的元素,因为访问元素,对于 Scala 来说是方法调用,(index) 相当于执行了 .apply(index) 方法。 Scala 中的数组与 Java 中的是等价的,Array[Int]() 在虚拟机层面就等价于
本文向大家介绍C语言 数组指针详解及示例代码,包括了C语言 数组指针详解及示例代码的使用技巧和注意事项,需要的朋友参考一下 数组(Array)是一系列具有相同类型的数据的集合,每一份数据叫做一个数组元素(Element)。数组中的所有元素在内存中是连续排列的,整个数组占用的是一块内存。以int arr[] = { 99, 15, 100, 888, 252 };为例,该数组在内存中的分布如下图所示