rapidjson将map转为json------人生苦短,我用rapidjson
直接撸代码:
#include #include //请自己下载开源的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" usingnamespacestd; usingrapidjson::Document; usingrapidjson::StringBuffer; usingrapidjson::Writer; usingnamespacerapidjson; stringtest(constmap&m)//注意这里的const { Documentdocument; Document::AllocatorType&allocator=document.GetAllocator(); Valueroot(kObjectType); Valuekey(kStringType); Valuevalue(kStringType); for(map::const_iteratorit=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); } StringBufferbuffer; Writerwriter(buffer); root.Accept(writer); returnbuffer.GetString(); } intmain(intargc,char*argv[]) { mapm; m["name"]="taoge"; m["place"]="shenzhen"; cout< 结果: {"name":"taoge","place":"shenzhen"} 来,继续改: #include #include //请自己下载开源的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" usingnamespacestd; usingrapidjson::Document; usingrapidjson::StringBuffer; usingrapidjson::Writer; usingnamespacerapidjson; stringtest(constmap&mInt,constmap&mString)//注意这里的const { Documentdocument; Document::AllocatorType&allocator=document.GetAllocator(); Valueroot(kObjectType); Valuekey(kStringType); Valuevalue(kStringType); for(map::const_iteratorit=mInt.begin();it!=mInt.end();++it)//注意这里要用const_iterator { key.SetString(it->first.c_str(),allocator); root.AddMember(key,it->second,allocator); } for(map::const_iteratorit=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); } StringBufferbuffer; Writerwriter(buffer); root.Accept(writer); returnbuffer.GetString(); } intmain(intargc,char*argv[]) { mapmInt; mInt["age"]=29; mInt["score"]=80; mapmString; mString["name"]="taoge"; mString["place"]="shenzhen"; cout< 结果: {"age":29,"score":80,"name":"taoge","place":"shenzhen"} 不多说。 总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
结果:
{"name":"taoge","place":"shenzhen"}
来,继续改:
#include #include //请自己下载开源的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" usingnamespacestd; usingrapidjson::Document; usingrapidjson::StringBuffer; usingrapidjson::Writer; usingnamespacerapidjson; stringtest(constmap&mInt,constmap&mString)//注意这里的const { Documentdocument; Document::AllocatorType&allocator=document.GetAllocator(); Valueroot(kObjectType); Valuekey(kStringType); Valuevalue(kStringType); for(map::const_iteratorit=mInt.begin();it!=mInt.end();++it)//注意这里要用const_iterator { key.SetString(it->first.c_str(),allocator); root.AddMember(key,it->second,allocator); } for(map::const_iteratorit=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); } StringBufferbuffer; Writerwriter(buffer); root.Accept(writer); returnbuffer.GetString(); } intmain(intargc,char*argv[]) { mapmInt; mInt["age"]=29; mInt["score"]=80; mapmString; mString["name"]="taoge"; mString["place"]="shenzhen"; cout< 结果: {"age":29,"score":80,"name":"taoge","place":"shenzhen"} 不多说。 总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
{"age":29,"score":80,"name":"taoge","place":"shenzhen"}
不多说。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接