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++中輸入一個字符串,把其中的字符按照逆序輸出的兩種方法進行了詳細的分析介紹,需要的朋友可以過來參考下2013-07-07
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

