rapidjson组装map和数组array的代码示例
直接上码:
#include #include //请自己下载开源的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 #include usingnamespacestd; usingrapidjson::Document; usingrapidjson::StringBuffer; usingrapidjson::Writer; usingnamespacerapidjson; //注意int和uint64_t mapg_mChildInt; mapg_mChildString; stringformJson(constmap&mInt,constmap&mString, conststring&strChild="",constmap&mChildInt=g_mChildInt,constmap&mChildString=g_mChildString, conststring&strChild2="",constmap&mChildInt2=g_mChildInt,constmap&mChildString2=g_mChildString) { Documentdocument; Document::AllocatorType&allocator=document.GetAllocator(); Valueroot(kObjectType); Valuekey(kStringType); Valuevalue(kStringType); //当前级别 for(map::const_iteratorit=mInt.begin();it!=mInt.end();++it) { key.SetString(it->first.c_str(),allocator); root.AddMember(key,it->second,allocator); } for(map::const_iteratorit=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()) { Valuechild(kObjectType); for(map::const_iteratorit=mChildInt.begin();it!=mChildInt.end();++it) { key.SetString(it->first.c_str(),allocator); child.AddMember(key,it->second,allocator); } for(map::const_iteratorit=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()) { Valuechild(kObjectType); for(map::const_iteratorit=mChildInt2.begin();it!=mChildInt2.end();++it) { key.SetString(it->first.c_str(),allocator); child.AddMember(key,it->second,allocator); } for(map::const_iteratorit=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); } StringBufferbuffer; Writerwriter(buffer); root.Accept(writer); returnbuffer.GetString(); } stringformJsonWithArray(constmap&mInt,constmap&mString, conststring&strChild1,constmap&mChildInt,constmap&mChildString, string&strChild2,vector>&mVecChildInt,vector>&mVecChildString) { Documentdocument; Document::AllocatorType&allocator=document.GetAllocator(); Valueroot(kObjectType); Valuekey(kStringType); Valuevalue(kStringType); //当前级别 for(map::const_iteratorit=mInt.begin();it!=mInt.end();++it) { key.SetString(it->first.c_str(),allocator); root.AddMember(key,it->second,allocator); } for(map::const_iteratorit=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()) { Valuechild(kObjectType); for(map::const_iteratorit=mChildInt.begin();it!=mChildInt.end();++it) { key.SetString(it->first.c_str(),allocator); child.AddMember(key,it->second,allocator); } for(map::const_iteratorit=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); } //孩子级别 unsignedintuiSize1=mVecChildInt.size(); unsignedintuiSize2=mVecChildString.size(); if(!strChild2.empty()&&uiSize1==uiSize2) { Valuearray(rapidjson::kArrayType); for(unsignedinti=0;i::iteratorit=mVecChildInt[i].begin();it!=mVecChildInt[i].end();++it) { key.SetString(it->first.c_str(),allocator); child.AddMember(key,it->second,allocator); } for(map::iteratorit=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); } StringBufferbuffer; Writerwriter(buffer); root.Accept(writer); returnbuffer.GetString(); } voidtest1() { mapmInt; mapmString; mInt["code"]=0; mString["msg"]="ok"; stringstrChild1="xxx"; mapmChildInt1; mapmChildString1; mChildInt1["key"]=729; mChildString1["kk"]="vv"; stringstrChild2="yyy"; mapmChildInt2; mapmChildString2; mChildInt2["key"]=730; mChildString2["kkk"]="vvv"; strings=formJson(mInt,mString,strChild1,mChildInt1,mChildString1,strChild2,mChildInt2,mChildString2); cout<mInt; mapmString; mInt["code"]=0; mString["msg"]="ok"; stringstrChild1="xxx"; mapmChildInt; mapmChildString; mChildString["kk"]="vv"; mChildInt["key"]=729; stringstrChild2="data"; vector>mVecChildInt; vector>mVecChildString; { mapmChildInt; mapmChildString; mChildInt["id"]=1; mChildString["path"]="pa"; mChildString["sha"]="sh"; mVecChildInt.push_back(mChildInt); mVecChildString.push_back(mChildString); } { mapmChildInt; mapmChildString; mChildInt["id"]=2; mChildString["path"]="pa"; mChildString["sha"]="sh"; mVecChildInt.push_back(mChildInt); mVecChildString.push_back(mChildString); } strings=formJsonWithArray(mInt,mString,strChild1,mChildInt,mChildString,strChild2,mVecChildInt,mVecChildString); cout< 结果: {"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"}]} 总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
结果:
{"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"}]}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接