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

C++中rapidjson將map轉(zhuǎn)為json的方法

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

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 test(const map<string, string> &m)  // 注意這里的const
{
  Document document; 
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType); 
  Value key(kStringType); 
  Value value(kStringType); 
 for(map<string, string>::const_iterator it = m.begin(); it != m.end(); ++it) // 注意這里要用const_iterator
 {
 key.SetString(it->first.c_str(), allocator); 
   value.SetString(it->second.c_str(), allocator); 
   root.AddMember(key, value, allocator);
 }
  StringBuffer buffer; 
  Writer<StringBuffer> writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
} 
int main(int argc, char *argv[])
{
 map<string, string> m;
 m["name"] = "taoge";
 m["place"] = "shenzhen";
 cout << test(m) << endl;
 return 0;
}

結(jié)果:

{"name":"taoge","place":"shenzhen"}

來,繼續(xù)改:

#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 test(const map<string, int> &mInt, const map<string, string> &mString)  // 注意這里的const
{
  Document document; 
  Document::AllocatorType& allocator = document.GetAllocator(); 
  Value root(kObjectType); 
  Value key(kStringType); 
  Value value(kStringType); 
 for(map<string, int>::const_iterator it = mInt.begin(); it != mInt.end(); ++it) // 注意這里要用const_iterator
 {
 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) // 注意這里要用const_iterator
 {
 key.SetString(it->first.c_str(), allocator); 
   value.SetString(it->second.c_str(), allocator); 
   root.AddMember(key, value, allocator);
 }
  StringBuffer buffer; 
  Writer<StringBuffer> writer(buffer); 
  root.Accept(writer); 
  return buffer.GetString(); 
} 
int main(int argc, char *argv[])
{
 map<string, int> mInt;
 mInt["age"] = 29;
 mInt["score"] = 80;
 map<string, string> mString;
 mString["name"] = "taoge";
 mString["place"] = "shenzhen";
 cout << test(mInt, mString) << endl;
 return 0;
}

結(jié)果:

{"age":29,"score":80,"name":"taoge","place":"shenzhen"}

不多說。

總結(jié)

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

相關(guān)文章

  • C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))

    C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C++中typedef 及其與struct的結(jié)合使用

    C++中typedef 及其與struct的結(jié)合使用

    這篇文章主要介紹了C++中typedef 及其與struct的結(jié)合使用,需要的朋友可以參考下
    2014-02-02
  • C++ 實(shí)現(xiàn)求小于n的最大素?cái)?shù)的實(shí)例

    C++ 實(shí)現(xiàn)求小于n的最大素?cái)?shù)的實(shí)例

    這篇文章主要介紹了C++ 實(shí)現(xiàn)求小于n的最大素?cái)?shù)的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • C++11中內(nèi)聯(lián)函數(shù)(inline)用法實(shí)例

    C++11中內(nèi)聯(lián)函數(shù)(inline)用法實(shí)例

    內(nèi)聯(lián)函數(shù)本質(zhì)還是一個函數(shù),但在聲明的時(shí)候,函數(shù)體要和聲明結(jié)合在一起,否則編譯器將它作為普通函數(shù)來對待,下面這篇文章主要給大家介紹了關(guān)于C++11中內(nèi)聯(lián)函數(shù)(inline)的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • C語言遞歸實(shí)現(xiàn)歸并排序詳解

    C語言遞歸實(shí)現(xiàn)歸并排序詳解

    這篇文章主要為大家詳細(xì)介紹了C語言遞歸實(shí)現(xiàn)歸并排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,?希望能夠給你帶來幫助
    2022-03-03
  • C語言實(shí)現(xiàn)打磚塊小游戲

    C語言實(shí)現(xiàn)打磚塊小游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)打磚塊小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C++與QML交互的項(xiàng)目實(shí)踐

    C++與QML交互的項(xiàng)目實(shí)踐

    本文主要介紹了C++與QML交互的項(xiàng)目實(shí)踐,將詳細(xì)介紹C++與QML的交互方式,包括在QML中調(diào)用C++函數(shù)和在C++中訪問QML元素,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • C++實(shí)現(xiàn)LeetCode(155.最小棧)

    C++實(shí)現(xiàn)LeetCode(155.最小棧)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(155.最小棧),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言編程中生成隨機(jī)數(shù)的入門教程

    C語言編程中生成隨機(jī)數(shù)的入門教程

    這篇文章主要介紹了C語言編程中生成隨機(jī)數(shù)的入門教程,包括利用rand()函數(shù)來編寫隨機(jī)數(shù)生成器的示例,要的朋友可以參考下
    2015-12-12
  • C語言實(shí)現(xiàn)簡單的文本編輯器

    C語言實(shí)現(xiàn)簡單的文本編輯器

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單的文本編輯器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05

最新評論