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

所属分类: 软件编程 / C 语言 阅读数: 67
收藏 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#复制和深度复制的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C# 使用反射来实现对象的深度复制方法

下面小编就为大家带来一篇C# 使用反射来实现对象的深度复制方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C语言基础 原码、反码、补码和移码详解

这篇文章主要介绍了C语言基础 原码、反码、补码和移码详解的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Species Tree 利用HashTable实现实例代码

这篇文章主要介绍了Species Tree 利用HashTable实现实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言 实现遍历一个文件夹的所有文件

这篇文章主要介绍了C语言 实现遍历一个文件夹的所有文件的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

C++11新特性中auto 和 decltype 区别和联系

这篇文章主要介绍了C++11新特性中auto 和 decltype 区别和联系的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

C++ 反射机制详解及实例代码

这篇文章主要介绍了C++ 反射机制详解及实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

C++中构造函数与析构函数的调用顺序详解

C++ 语言一直被批评太复杂了,很多细节的地方需要仔细推敲,甚至其构造函数和析构的调用顺序也成为了一个让人迷惑的问题,在此我做了简单的总结。这篇文章主要介绍了C++中构造函数与析构函数的调用顺序,需要的朋友可以参考借鉴。
收藏 0 赞 0 分享

c#中实现退出程序后自动重新启动程序的方法

下面小编就为大家带来一篇c#中实现退出程序后自动重新启动程序的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C语言中时间的基本用法小结

处理时间是编程中经常遇到的问题,C语言中提供了一些时间处理函数,在此记录下一些基本的用法。下面这篇文章主要给大家介绍了C语言中关于时间的基本用法的相关资料,需要的朋友可以参考借鉴,感兴趣的朋友们来一起看看吧。
收藏 0 赞 0 分享
查看更多