国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

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

2020-01-26 13:29:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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ì)武林網(wǎng)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 和平县| 通化县| 突泉县| 德安县| 衡南县| 广西| 浑源县| 广安市| 惠来县| 孝义市| 龙胜| 青阳县| 定南县| 行唐县| 清新县| 都安| 绥滨县| 舞阳县| 综艺| 郯城县| 象州县| 哈密市| 秦皇岛市| 开原市| 余江县| 临汾市| 通许县| 太原市| 海口市| 余姚市| 浪卡子县| 会宁县| 民丰县| 绥滨县| 远安县| 葵青区| 霸州市| 察哈| 乳源| 富宁县| 甘泉县|