rapidjson将map转为json------人生苦短,我用rapidjson
来源:互联网 发布:写java用什么eclipse 编辑:程序博客网 时间:2024/06/07 07:30
直接撸代码:
#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"}
不多说。
阅读全文
1 0
- rapidjson将map转为json------人生苦短,我用rapidjson
- rapidjson将嵌套map转为嵌套json------人生苦短,我用rapidjson
- rapidjson组装继续简化------人生苦短,我用rapidjson
- rapidjson
- rapidjson建立json
- Json 和 RapidJson
- 人生苦短,我用Python
- 人生苦短,我用 Python
- 人生苦短我用Python
- 人生苦短,我用Python!
- 人生苦短我用Chrome
- 人生苦短,我用 Python
- 人生苦短,我用python
- 人生苦短,我用python
- 人生苦短,我用python
- c++解析json文件: Rapidjson
- json: RapidJson 解析(1)
- 使用rapidJson解析json文件
- 01背包记录路径)
- 宽度已知,实现三栏布局的方案(5种layout方案)
- Linux服务器Zookeeper+Dubbo环境搭建
- java基础语法之自加(++)自减(--)讲解-----看了就会!!!
- vue.js开发外卖App项目总结(二)
- rapidjson将map转为json------人生苦短,我用rapidjson
- 位压缩
- oracle--认识表与约束
- 单元测试多线程输出问题
- 投票决策系统的理论基础
- 亲加直播代码混淆
- Java编程思想---第一章笔记
- 从零基础认识事务
- 70. Climbing Stairs