C++中rapidjson將map轉(zhuǎn)為json的方法
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 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"}
來(lái),繼續(xù)改:
#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 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"}
不多說(shuō)。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C++哈希應(yīng)用之位圖,哈希切分與布隆過(guò)濾器詳解
這篇文章主要為大家詳細(xì)介紹了C++哈希應(yīng)用中的位圖、哈希切分與布隆過(guò)濾器,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下2023-04-04
QML中動(dòng)態(tài)與靜態(tài)模型應(yīng)用詳解
QML是一種描述性的腳本語(yǔ)言,文件格式以.qml結(jié)尾。語(yǔ)法格式非常像CSS(參考后文具體例子),但又支持javascript形式的編程控制。QtDesigner可以設(shè)計(jì)出·ui界面文件,但是不支持和Qt原生C++代碼的交互2022-08-08
C++輸入一個(gè)字符串,把其中的字符按照逆序輸出的兩種方法解析
以下是對(duì)C++中輸入一個(gè)字符串,把其中的字符按照逆序輸出的兩種方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-07-07
Qt利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Qt如何利用tablewidget模擬手指實(shí)現(xiàn)滑動(dòng)效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定的幫助,需要的可以參考一下2023-01-01
C語(yǔ)言設(shè)計(jì)實(shí)現(xiàn)掃描器的自動(dòng)機(jī)的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言設(shè)計(jì)實(shí)現(xiàn)掃描器的自動(dòng)機(jī),可識(shí)別的單詞包括:關(guān)鍵字、界符、標(biāo)識(shí)符和常整型數(shù),感興趣的小伙伴可以了解一下2022-12-12
C/C++實(shí)現(xiàn)發(fā)送與接收HTTP/S請(qǐng)求的示例代碼
HTTP(Hypertext Transfer Protocol)是一種用于傳輸超文本的協(xié)議,它是一種無(wú)狀態(tài)的、應(yīng)用層的協(xié)議,用于在計(jì)算機(jī)之間傳輸超文本文檔,通常在 Web 瀏覽器和 Web 服務(wù)器之間進(jìn)行數(shù)據(jù)通信,本文給大家介紹了C/C++發(fā)送與接收HTTP/S請(qǐng)求,需要的朋友可以參考下2023-11-11
C語(yǔ)言中聯(lián)合體union的實(shí)例詳解
這篇文章主要介紹了 C語(yǔ)言中聯(lián)合體union的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10

