C++中rapidjson将map转为json的方法
rapidjson将map转为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; }
结果:
{"name":"taoge","place":"shenzhen"}
来,继续改:
#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; }
结果:
{"age":29,"score":80,"name":"taoge","place":"shenzhen"}
不多说。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接
栏 目:C语言
下一篇:详解C++基础——类继承
本文标题:C++中rapidjson将map转为json的方法
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/331.html
您可能感兴趣的文章
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02c语言没有round函数 round c语言
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10深入Main函数中的参数argc,argv的使用详解
- 01-10APUE笔记之:进程环境详解
- 01-10c++中inline的用法分析
- 01-10如何寻找数组中的第二大数


阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10C#中split用法实例总结
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05dedecms(织梦)副栏目数量限制代码修改