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

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

 更新時間:2019年04月08日 10:45:54   作者: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 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)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • C++哈希應用之位圖,哈希切分與布隆過濾器詳解

    C++哈希應用之位圖,哈希切分與布隆過濾器詳解

    這篇文章主要為大家詳細介紹了C++哈希應用中的位圖、哈希切分與布隆過濾器,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下
    2023-04-04
  • QML中動態(tài)與靜態(tài)模型應用詳解

    QML中動態(tài)與靜態(tài)模型應用詳解

    QML是一種描述性的腳本語言,文件格式以.qml結(jié)尾。語法格式非常像CSS(參考后文具體例子),但又支持javascript形式的編程控制。QtDesigner可以設計出·ui界面文件,但是不支持和Qt原生C++代碼的交互
    2022-08-08
  • 示例詳解C++中的各種鎖

    示例詳解C++中的各種鎖

    C++中常見的鎖包括互斥鎖、遞歸互斥鎖、讀寫鎖、定時互斥鎖、遞歸定時互斥鎖、自旋鎖和條件變量,互斥鎖用于防止多線程同時訪問共享資源,遞歸互斥鎖允許同一線程多次獲取鎖,讀寫鎖區(qū)分讀寫操作,提高并發(fā)性
    2024-11-11
  • C++輸入一個字符串,把其中的字符按照逆序輸出的兩種方法解析

    C++輸入一個字符串,把其中的字符按照逆序輸出的兩種方法解析

    以下是對C++中輸入一個字符串,把其中的字符按照逆序輸出的兩種方法進行了詳細的分析介紹,需要的朋友可以過來參考下
    2013-07-07
  • Qt利用tablewidget模擬手指實現(xiàn)滑動

    Qt利用tablewidget模擬手指實現(xiàn)滑動

    這篇文章主要為大家詳細介紹了Qt如何利用tablewidget模擬手指實現(xiàn)滑動效果,文中的示例代碼講解詳細,對我們學習Qt有一定的幫助,需要的可以參考一下
    2023-01-01
  • C語言設計實現(xiàn)掃描器的自動機的示例詳解

    C語言設計實現(xiàn)掃描器的自動機的示例詳解

    這篇文章主要為大家詳細介紹了如何利用C語言設計實現(xiàn)掃描器的自動機,可識別的單詞包括:關(guān)鍵字、界符、標識符和常整型數(shù),感興趣的小伙伴可以了解一下
    2022-12-12
  • C/C++實現(xiàn)發(fā)送與接收HTTP/S請求的示例代碼

    C/C++實現(xiàn)發(fā)送與接收HTTP/S請求的示例代碼

    HTTP(Hypertext Transfer Protocol)是一種用于傳輸超文本的協(xié)議,它是一種無狀態(tài)的、應用層的協(xié)議,用于在計算機之間傳輸超文本文檔,通常在 Web 瀏覽器和 Web 服務器之間進行數(shù)據(jù)通信,本文給大家介紹了C/C++發(fā)送與接收HTTP/S請求,需要的朋友可以參考下
    2023-11-11
  • C++堆和棧的區(qū)別與聯(lián)系講解

    C++堆和棧的區(qū)別與聯(lián)系講解

    今天小編就為大家分享一篇關(guān)于C++堆和棧的區(qū)別與聯(lián)系講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Qt讀寫XML文件的方法詳解(含源碼+注釋)

    Qt讀寫XML文件的方法詳解(含源碼+注釋)

    XML文件可以用來存儲項目中的數(shù)據(jù),它相當于一個簡單的數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Qt讀寫XML文件(含源碼+注釋)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-10-10
  • C語言中聯(lián)合體union的實例詳解

    C語言中聯(lián)合體union的實例詳解

    這篇文章主要介紹了 C語言中聯(lián)合體union的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10

最新評論

江西省| 陇西县| 鹿泉市| 金昌市| 建德市| 南郑县| 嘉兴市| 汕头市| 柳河县| 安义县| 兴化市| 汉沽区| 南汇区| 西乌珠穆沁旗| 方城县| 独山县| 安康市| 仲巴县| 奈曼旗| 江西省| 安达市| 武陟县| 遵义市| 班戈县| 广南县| 南城县| 偃师市| 获嘉县| 和田市| 东兰县| 大渡口区| 芦山县| 泗水县| 中卫市| 贺州市| 宜兰县| 临高县| 富川| 芦山县| 包头市| 南雄市|