C++中rapidjson將map轉(zhuǎn)為json的方法
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)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C++中typedef 及其與struct的結(jié)合使用
這篇文章主要介紹了C++中typedef 及其與struct的結(jié)合使用,需要的朋友可以參考下2014-02-02C++ 實(shí)現(xiàn)求小于n的最大素?cái)?shù)的實(shí)例
這篇文章主要介紹了C++ 實(shí)現(xiàn)求小于n的最大素?cái)?shù)的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05C++11中內(nèi)聯(lián)函數(shù)(inline)用法實(shí)例
內(nèi)聯(lián)函數(shù)本質(zhì)還是一個函數(shù),但在聲明的時(shí)候,函數(shù)體要和聲明結(jié)合在一起,否則編譯器將它作為普通函數(shù)來對待,下面這篇文章主要給大家介紹了關(guān)于C++11中內(nèi)聯(lián)函數(shù)(inline)的相關(guān)資料,需要的朋友可以參考下2022-10-10C++實(shí)現(xiàn)LeetCode(155.最小棧)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(155.最小棧),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07