C++中rapidjson组装继续简化的方法

所属分类: 软件编程 / C 语言 阅读数: 59
收藏 0 赞 0 分享

rapidjson组装继续简化------人生苦短,我用rapidjson

看最简单的:

#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 请自己下载开源的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;
void test()
{
 Document document;
 document.SetObject();
 Document::AllocatorType& allocator = document.GetAllocator();
 Value object(rapidjson::kObjectType);
 document.AddMember("age", 29, allocator);
 document.AddMember("name", "taoge", allocator);
 StringBuffer buffer;
 Writer<StringBuffer> writer(buffer);
 document.Accept(writer);
 string str = buffer.GetString();
 cout << str << endl;
}
int main(int argc, char *argv[])
{
 test();
 return 0;
}

结果:

{"age":29,"name":"taoge"}

再看数组:

#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 请自己下载开源的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;
void test()
{
 Document document;
 document.SetObject();
 Document::AllocatorType& allocator = document.GetAllocator();
 Value array(rapidjson::kArrayType);
 Value object(rapidjson::kObjectType);
 object.AddMember("age", 30, allocator);
 object.AddMember("name", "taoge", allocator);
 array.PushBack(object, allocator);
 document.AddMember("json", array, allocator);
 StringBuffer buffer;
 Writer<StringBuffer> writer(buffer);
 document.Accept(writer);
 string str = buffer.GetString();
 cout << str << endl;
}
int main(int argc, char *argv[])
{
 test();
 return 0;
}

结果:

{"json":[{"age":30,"name":"taoge"}]}

再来看一个:

#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 请自己下载开源的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;
void test()
{
 Document document;
 document.SetObject();
 Document::AllocatorType& allocator = document.GetAllocator();
 Value array(rapidjson::kArrayType);
 Value object(rapidjson::kObjectType);
 object.AddMember("age", 30, allocator);
 object.AddMember("name", "taoge", allocator);
 array.PushBack(object, allocator);
 document.AddMember("oh1", array, allocator);
 document.AddMember("oh2", "hehe", allocator);
 StringBuffer buffer;
 Writer<StringBuffer> writer(buffer);
 document.Accept(writer);
 string str = buffer.GetString();
 cout << str << endl;
}
int main(int argc, char *argv[])
{
 test();
 return 0;
}

结果:

{"oh1":[{"age":30,"name":"taoge"}],"oh2":"hehe"}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

更多精彩内容其他人还在看

C++获得其他程序窗体控件中信息的方法

这篇文章主要介绍了C++获得其他程序窗体控件中信息的方法,涉及windows控件句柄的获取技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现简单遗传算法

这篇文章主要介绍了C++实现简单遗传算法,以实例形式较为详细的分析了遗传算法的C++实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++联合体union用法实例详解

这篇文章主要介绍了C++联合体union用法,较为详细的分析了C++中联合体的概念、实用技巧及相关注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

C/C++实现的游戏角色名称名字随机生成代码

这篇文章主要介绍了C/C++实现的游戏角色名称名字随机生成代码,本文特别针对一些古典游戏的角色名称进行随机生成,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言高效编程的几招小技巧

这篇文章主要介绍了C语言高效编程的几招小技巧,本文讲解了以空间换时间、用数学方法解决问题以及使用位操作等编辑技巧,并给出若干方法和代码实例,需要的朋友可以参考下
收藏 0 赞 0 分享

C++二分查找在搜索引擎多文档求交的应用分析

这篇文章主要介绍了C++二分查找在搜索引擎多文档求交的应用,实例分析了二分查找的原理与C++的实现及应用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C++不使用变量求字符串长度strlen函数的实现方法

这篇文章主要介绍了C++不使用变量求字符串长度strlen函数的实现方法,实例分析了strlen函数的实现原理与不使用变量求字符串长度的实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C++通过自定义函数找出一个整数数组中第二大数的方法

这篇文章主要介绍了C++通过自定义函数找出一个整数数组中第二大数的方法,涉及C++针对数组的遍历操作相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的数组和指针汇编代码分析实例

这篇文章主要介绍了C语言中的数组和指针汇编代码分析实例,本文用一则C语言例子来得到对应的汇编代码,并一一注解每句汇编代码的含义,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++时钟函数

VC中提供了很多关于时间操作的函数,编写程序时我们可以跟据定时的不同精度要求选择不同的时间函数来完成定时和计时操作
收藏 0 赞 0 分享
查看更多