欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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++智能指針shared_ptr

    C++智能指針shared_ptr

    這篇文章主要介紹了C++智能指針shared_ptr,C++11中包括shared_ptr在內(nèi)的多種指針,都是模板類型,意味著使用者可以指定想要操作的類型下文從shared_ptr創(chuàng)建方式展開(kāi)全文,介紹詳細(xì)具有一的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • C++內(nèi)存管理詳解使用方式

    C++內(nèi)存管理詳解使用方式

    內(nèi)存管理是C++最令人切齒痛恨的問(wèn)題,也是C++最有爭(zhēng)議的問(wèn)題,C++高手從中獲得了更好的性能更大的自由,C++菜鳥(niǎo)的收獲則是一遍—遍的檢查代碼和對(duì)C++的痛恨,但內(nèi)存管理在C++中無(wú)處不在,內(nèi)存泄漏幾乎在每個(gè)C++程序中都會(huì)發(fā)生,要想成為C++高手,內(nèi)存管理這關(guān)是必須過(guò)的
    2022-04-04
  • C++?多繼承詳情介紹

    C++?多繼承詳情介紹

    這篇文章主要介紹了C++?多繼承詳情,C++支持多繼承,即允許一個(gè)類同時(shí)繼承多個(gè)類。只有C++等少數(shù)語(yǔ)言支持多繼承,下面我們就來(lái)看看具體的多繼承介紹吧,需要的朋友可以參考一下
    2022-03-03
  • OPENMP?SECTIONS?CONSTRUCT原理示例解析

    OPENMP?SECTIONS?CONSTRUCT原理示例解析

    這篇文章主要為大家介紹了OPENMP?SECTIONS?CONSTRUCT原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 全面了解C語(yǔ)言?static?關(guān)鍵字

    全面了解C語(yǔ)言?static?關(guān)鍵字

    這篇文章主要介紹了全面了解C語(yǔ)言?static?關(guān)鍵字,文章首先通過(guò)先介紹一下頭文件的創(chuàng)建展開(kāi)主題的詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-04-04
  • C++入門之模板基礎(chǔ)講解

    C++入門之模板基礎(chǔ)講解

    這篇文章主要為大家介紹了C++入門之模板基礎(chǔ),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • C++實(shí)現(xiàn)簡(jiǎn)單遺傳算法

    C++實(shí)現(xiàn)簡(jiǎn)單遺傳算法

    這篇文章主要介紹了C++實(shí)現(xiàn)簡(jiǎn)單遺傳算法,以實(shí)例形式較為詳細(xì)的分析了遺傳算法的C++實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C++實(shí)現(xiàn)大數(shù)相乘算法

    C++實(shí)現(xiàn)大數(shù)相乘算法

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)大數(shù)相乘算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • C語(yǔ)言實(shí)現(xiàn)航班訂票系統(tǒng)

    C語(yǔ)言實(shí)現(xiàn)航班訂票系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)航班訂票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C++實(shí)現(xiàn)LeetCode(55.跳躍游戲)

    C++實(shí)現(xiàn)LeetCode(55.跳躍游戲)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(55.跳躍游戲),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07

最新評(píng)論