C++中rapidjson組裝map和數(shù)組array的代碼示例
rapidjson組裝map和數(shù)組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);
// 當(dāng)前級別
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);
// 當(dāng)前級別
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;
}
結(jié)果:
{"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"}]}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對我們的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
欄 目:C語言
下一篇:c++文件監(jiān)控之FileSystemWatcher
本文標題:C++中rapidjson組裝map和數(shù)組array的代碼示例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/360.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子


