C++中rapidjson將嵌套map轉(zhuǎn)為嵌套json的講解
rapidjson將嵌套map轉(zhuǎn)為嵌套json------人生苦短,我用rapidjson
看代碼:
#include <iostream>
#include <map>
// 請(qǐng)自己下載開(kāi)源的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);
// 當(dāng)前級(jí)別
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);
}
// 孩子級(jí)別
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;
}
結(jié)果:
{"code":0,"score":80,"name":"taoge","place":"shenzhen","childNode":{"code":0,"score":100,"name":"taogeChild","place":"shenzhen"}}
另外, 如果僅僅想有當(dāng)前界別, 那么, 可以這么搞(C++默認(rèn)參數(shù)搞起):
#include <iostream>
#include <map>
// 請(qǐng)自己下載開(kāi)源的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);
// 當(dāng)前級(jí)別
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);
}
// 孩子級(jí)別
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;
}
結(jié)果:
{"code":0,"score":80,"name":"taoge","place":"shenzhen"}
其實(shí), 上面的formJson函數(shù), 還可以繼續(xù)擴(kuò)展。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C語(yǔ)言進(jìn)程程序替換的實(shí)現(xiàn)詳解
為什么要進(jìn)程替換?因?yàn)楦高M(jìn)程創(chuàng)建出來(lái)的子進(jìn)程和父進(jìn)程擁有相同的代碼段,所以,子進(jìn)程看到的代碼和父進(jìn)程是一樣的。當(dāng)我們想要讓子進(jìn)程執(zhí)行不同的程序時(shí)候,就需要讓子進(jìn)程調(diào)用進(jìn)程程序替換的接口,從而讓子進(jìn)程執(zhí)行不一樣的代碼2022-08-08
詳解C++中二進(jìn)制求補(bǔ)運(yùn)算符與下標(biāo)運(yùn)算符的用法
這篇文章主要介紹了C++中二進(jìn)制求補(bǔ)運(yùn)算符與下標(biāo)運(yùn)算符的用法,是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-01-01
C++制作鼠標(biāo)連點(diǎn)器實(shí)例代碼
大家好,本篇文章主要講的是C++制作鼠標(biāo)連點(diǎn)器實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01
C++11中bind綁定器和function函數(shù)對(duì)象介紹
這篇文章主要介紹了C++11中bind綁定器和function函數(shù)對(duì)象介紹,綁定器,函數(shù)對(duì)象和lambda表達(dá)式只能使用在一條語(yǔ)句中,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-07-07
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單推箱子小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
C++實(shí)現(xiàn)LeetCode(62.不同的路徑)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(62.不同的路徑),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言求素?cái)?shù)的幾種方式總結(jié)
這篇文章主要介紹了C語(yǔ)言求素?cái)?shù)的幾種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

