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 formJson(const map<string, int> &mInt, const map<string, string> &mString, const string &strChild, const map<string, int> &mChildInt, const map<string, string> &mChildString) { Document document; Document::AllocatorType& allocator = document.GetAllocator(); Value root(kObjectType); Value child(kObjectType); Value key(kStringType); Value value(kStringType); // 当前级别 for(map<string, int>::const_iterator it = mInt.begin(); it != mInt.end(); ++it) { 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) { key.SetString(it->first.c_str(), allocator); value.SetString(it->second.c_str(), allocator); root.AddMember(key, value, allocator); } // 孩子级别 if(!strChild.empty()) { for(map<string, int>::const_iterator it = mChildInt.begin(); it != mChildInt.end(); ++it) { key.SetString(it->first.c_str(), allocator); child.AddMember(key, it->second, allocator); } for(map<string, string>::const_iterator it = mChildString.begin(); it != mChildString.end(); ++it) { key.SetString(it->first.c_str(), allocator); value.SetString(it->second.c_str(), allocator); child.AddMember(key, value, allocator); } key.SetString(strChild.c_str(), allocator); root.AddMember(key, child, allocator); } StringBuffer buffer; Writer<StringBuffer> writer(buffer); root.Accept(writer); return buffer.GetString(); } int main(int argc, char *argv[]) { map<string, int> mInt; mInt["code"] = 0; mInt["score"] = 80; map<string, string> mString; mString["name"] = "taoge"; mString["place"] = "shenzhen"; string strChild = "childNode"; map<string, int> mChildInt; mChildInt["code"] = 0; mChildInt["score"] = 100; map<string, string> mChildString; mChildString["name"] = "taogeChild"; mChildString["place"] = "shenzhen"; string strJson = formJson(mInt, mString, strChild, mChildInt, mChildString); cout << strJson << endl; return 0; }
结果:
{"code":0,"score":80,"name":"taoge","place":"shenzhen","childNode":{"code":0,"score":100,"name":"taogeChild","place":"shenzhen"}}
另外, 如果仅仅想有当前界别, 那么, 可以这么搞(C++默认参数搞起):
#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; map<string, int> g_mChildInt; map<string, string> g_mChildString; string formJson(const map<string, int> &mInt, const map<string, string> &mString, const string &strChild="", const map<string, int> &mChildInt=g_mChildInt, const map<string, string> &mChildString=g_mChildString) { Document document; Document::AllocatorType& allocator = document.GetAllocator(); Value root(kObjectType); Value child(kObjectType); Value key(kStringType); Value value(kStringType); // 当前级别 for(map<string, int>::const_iterator it = mInt.begin(); it != mInt.end(); ++it) { 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) { key.SetString(it->first.c_str(), allocator); value.SetString(it->second.c_str(), allocator); root.AddMember(key, value, allocator); } // 孩子级别 if(!strChild.empty()) { for(map<string, int>::const_iterator it = mChildInt.begin(); it != mChildInt.end(); ++it) { key.SetString(it->first.c_str(), allocator); child.AddMember(key, it->second, allocator); } for(map<string, string>::const_iterator it = mChildString.begin(); it != mChildString.end(); ++it) { key.SetString(it->first.c_str(), allocator); value.SetString(it->second.c_str(), allocator); child.AddMember(key, value, allocator); } key.SetString(strChild.c_str(), allocator); root.AddMember(key, child, allocator); } StringBuffer buffer; Writer<StringBuffer> writer(buffer); root.Accept(writer); return buffer.GetString(); } int main(int argc, char *argv[]) { map<string, int> mInt; mInt["code"] = 0; mInt["score"] = 80; map<string, string> mString; mString["name"] = "taoge"; mString["place"] = "shenzhen"; string strJson = formJson(mInt, mString); cout << strJson << endl; return 0; }
结果:
{"code":0,"score":80,"name":"taoge","place":"shenzhen"}
其实, 上面的formJson函数, 还可以继续扩展。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接
栏 目:C语言
下一篇:rapidjson解析json代码实例以及常见的json core dump问题
本文标题:C++中rapidjson将嵌套map转为嵌套json的讲解
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/339.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语言调用函数求
随机阅读
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10C#中split用法实例总结
- 01-10SublimeText编译C开发环境设置
- 04-02jquery与jsp,用jquery
- 01-11ajax实现页面的局部加载
- 01-10delphi制作wav文件的方法
- 08-05DEDE织梦data目录下的sessions文件夹有什