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

C++中rapidjson組裝map和數(shù)組array的代碼示例

 更新時(shí)間:2019年04月08日 10:34:13   作者:stpeace  
今天小編就為大家分享一篇關(guān)于C++中rapidjson組裝map和數(shù)組array的代碼示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

rapidjson組裝map和數(shù)組array的代碼示例

直接上碼:

#include <iostream>
#include <map>
// 請(qǐng)自己下載開源的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)前級(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())
 {
 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);
 }
 // 孩子級(jí)別
 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)前級(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(!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);
 }
 // 孩子級(jí)別
 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é)

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

相關(guān)文章

  • 詳解C++ string常用截取字符串方法

    詳解C++ string常用截取字符串方法

    這篇文章主要介紹了C++ string常用截取字符串方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • C語(yǔ)言不用鏈表完成學(xué)生管理系統(tǒng)(完整代碼)

    C語(yǔ)言不用鏈表完成學(xué)生管理系統(tǒng)(完整代碼)

    這篇文章主要介紹了C語(yǔ)言不用鏈表完成學(xué)生管理系統(tǒng)(完整代碼),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • QT中線程池QThreadPool類概念和使用方法詳解

    QT中線程池QThreadPool類概念和使用方法詳解

    這篇文章主要為大家介紹了QT中線程池QThreadPool類概念和使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • C語(yǔ)言字符串?dāng)?shù)組詳解

    C語(yǔ)言字符串?dāng)?shù)組詳解

    這篇文章主要介紹了C語(yǔ)言字符串?dāng)?shù)組詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C++?11新特性之右值引用使用案例與應(yīng)用場(chǎng)景

    C++?11新特性之右值引用使用案例與應(yīng)用場(chǎng)景

    右值引用和move語(yǔ)義是C++ 11中重要的特性之一,可以提高程序的效率和性能,右值引用是一種新的引用類型,下面這篇文章主要給大家介紹了關(guān)于C++?11新特性之右值引用使用案例與應(yīng)用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Qt利用QState狀態(tài)機(jī)實(shí)現(xiàn)控件互斥操作詳解

    Qt利用QState狀態(tài)機(jī)實(shí)現(xiàn)控件互斥操作詳解

    這篇文章主要為大家詳細(xì)介紹了Qt如何利用QState狀態(tài)機(jī)實(shí)現(xiàn)控件互斥操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12
  • C++模板二段名字查找方法

    C++模板二段名字查找方法

    下面小編就為大家?guī)?lái)一篇C++模板二段名字查找方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • C++實(shí)現(xiàn)softmax函數(shù)的面試經(jīng)驗(yàn)

    C++實(shí)現(xiàn)softmax函數(shù)的面試經(jīng)驗(yàn)

    這篇文章主要為大家介紹了C++實(shí)現(xiàn)softmax函數(shù)的面試經(jīng)驗(yàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 單元測(cè)試代碼覆蓋率解析

    單元測(cè)試代碼覆蓋率解析

    單元測(cè)試(unit testing),是指對(duì)軟件中的最小可測(cè)試單元進(jìn)行檢查和驗(yàn)證。動(dòng)態(tài)分析就是通過(guò)觀察軟件運(yùn)行時(shí)的動(dòng)作,來(lái)提供執(zhí)行跟蹤,時(shí)間分析,以及測(cè)試覆蓋度方面的信息。下面我們來(lái)詳細(xì)了解下吧
    2019-06-06
  • C語(yǔ)言中讀寫交替時(shí)出現(xiàn)的問(wèn)題分析

    C語(yǔ)言中讀寫交替時(shí)出現(xiàn)的問(wèn)題分析

    讀寫命令交替,一定要使用fseek重新定位,否則出現(xiàn)輸入顯示混亂,這篇文章主要介紹了C語(yǔ)言中讀寫交替時(shí)出現(xiàn)的問(wèn)題分析,需要的朋友可以參考下
    2022-12-12

最新評(píng)論

从化市| 红桥区| 营山县| 法库县| 任丘市| 蒲城县| 镇坪县| 怀集县| 福安市| 桑日县| 枞阳县| 汕尾市| 海盐县| 南城县| 宝丰县| 娱乐| 体育| 阳泉市| 定襄县| 满洲里市| 湘潭县| 介休市| 桑日县| 五指山市| 文水县| 洛宁县| 池州市| 温泉县| 惠东县| 东光县| 顺昌县| 治多县| 湖口县| 大同县| 新晃| 河津市| 中方县| 绥化市| 庄浪县| 固安县| 江西省|