最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中rapidjson將嵌套map轉(zhuǎn)為嵌套json的講解

 更新時(shí)間:2019年04月08日 11:01:43   作者:stpeace  
今天小編就為大家分享一篇關(guān)于C++中rapidjson將嵌套map轉(zhuǎn)為嵌套json的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

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)詳解

    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++中二進(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í)例代碼

    大家好,本篇文章主要講的是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ì)象介紹

    這篇文章主要介紹了C++11中bind綁定器和function函數(shù)對(duì)象介紹,綁定器,函數(shù)對(duì)象和lambda表達(dá)式只能使用在一條語(yǔ)句中,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-07-07
  • 一篇文章帶你了解C語(yǔ)言函數(shù)遞歸

    一篇文章帶你了解C語(yǔ)言函數(shù)遞歸

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言函數(shù)遞歸,使用數(shù)據(jù)庫(kù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單推箱子小游戲

    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.不同的路徑)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(62.不同的路徑),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 詳解QML?調(diào)用?C++?中的內(nèi)容

    詳解QML?調(diào)用?C++?中的內(nèi)容

    這篇文章主要介紹了QML?怎么調(diào)用?C++?中的內(nèi)容,這里主要是總結(jié)一下,怎么在 QML 文件中引用 C ++ 文件里定義的內(nèi)容,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • C語(yǔ)言求素?cái)?shù)的幾種方式總結(jié)

    C語(yǔ)言求素?cái)?shù)的幾種方式總結(jié)

    這篇文章主要介紹了C語(yǔ)言求素?cái)?shù)的幾種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • c++中的兩種getline用法詳解

    c++中的兩種getline用法詳解

    c++中有2種getline函數(shù),一種在頭文件 <istream> 中,是istream類的成員函數(shù);另一種是在頭文件 <string> 中,是普通函數(shù)。這篇文章主要介紹了c++中的兩種getline用法,需要的朋友可以參考下
    2020-02-02

最新評(píng)論

澄迈县| 嘉兴市| 广饶县| 永昌县| 招远市| 巩义市| 蒙自县| 湖州市| 武威市| 新沂市| 沾化县| 崇义县| 扶沟县| 理塘县| 贵定县| 深水埗区| 平塘县| 延长县| 保亭| 楚雄市| 灵石县| 土默特左旗| 南郑县| 民权县| 长沙县| 郓城县| 石楼县| 郑州市| 凉山| 开封市| 光泽县| 新田县| 彩票| 法库县| 淮北市| 谷城县| 景谷| 崇仁县| 宿迁市| 高青县| 广丰县|