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

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

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

rapidjson將嵌套map轉(zhuǎn)為嵌套json------人生苦短,我用rapidjson

看代碼:

#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"
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)前級別
 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())
 {
 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>
// 請自己下載開源的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)前級別
 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())
 {
 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"}

其實, 上面的formJson函數(shù), 還可以繼續(xù)擴展。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • C語言進(jìn)程程序替換的實現(xiàn)詳解

    C語言進(jìn)程程序替換的實現(xiàn)詳解

    為什么要進(jìn)程替換?因為父進(jìn)程創(chuàng)建出來的子進(jìn)程和父進(jìn)程擁有相同的代碼段,所以,子進(jìn)程看到的代碼和父進(jìn)程是一樣的。當(dāng)我們想要讓子進(jìn)程執(zhí)行不同的程序時候,就需要讓子進(jìn)程調(diào)用進(jìn)程程序替換的接口,從而讓子進(jìn)程執(zhí)行不一樣的代碼
    2022-08-08
  • 詳解C++中二進(jìn)制求補運算符與下標(biāo)運算符的用法

    詳解C++中二進(jìn)制求補運算符與下標(biāo)運算符的用法

    這篇文章主要介紹了C++中二進(jìn)制求補運算符與下標(biāo)運算符的用法,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2016-01-01
  • C++制作鼠標(biāo)連點器實例代碼

    C++制作鼠標(biāo)連點器實例代碼

    大家好,本篇文章主要講的是C++制作鼠標(biāo)連點器實例代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • C++11中bind綁定器和function函數(shù)對象介紹

    C++11中bind綁定器和function函數(shù)對象介紹

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

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

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

    C語言實現(xiàn)簡單推箱子小游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • C++實現(xiàn)LeetCode(62.不同的路徑)

    C++實現(xiàn)LeetCode(62.不同的路徑)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(62.不同的路徑),本篇文章通過簡要的案例,講解了該項技術(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)容,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • C語言求素數(shù)的幾種方式總結(jié)

    C語言求素數(shù)的幾種方式總結(jié)

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

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

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

最新評論

黑河市| 基隆市| 常宁市| 集安市| 义马市| 万宁市| 海门市| 樟树市| 隆化县| 册亨县| 菏泽市| 德保县| 佛山市| 万盛区| 诸暨市| 甘孜县| 蓝山县| 唐山市| 达拉特旗| 湘阴县| 奉新县| 于田县| 抚松县| 大同县| 枞阳县| 花莲县| 伊川县| 九寨沟县| 隆尧县| 攀枝花市| 江川县| 宁强县| 丰顺县| 娄底市| 安福县| 禹城市| 桃江县| 香港 | 建湖县| 隆回县| 固原市|