国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 巴楚县| 达日县| 威信县| 黑河市| 新沂市| 满城县| 上蔡县| 白山市| 惠东县| 图们市| 石首市| 西贡区| 武强县| 合川市| 宁国市| 郎溪县| 日土县| 许昌市| 安义县| 措勤县| 霍城县| 梅河口市| 浏阳市| 裕民县| 四会市| 安仁县| 醴陵市| 都兰县| 理塘县| 中江县| 云安县| 东丽区| 高要市| 巴里| 儋州市| 七台河市| 宜宾县| 如皋市| 互助| 尼勒克县| 宁安市|