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

所属分类: 软件编程 / C 语言 阅读数: 125
收藏 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 分享

C++ 二叉搜索树(BST)的实现方法

这篇文章主要介绍了C++ 二叉搜索树(BST)的实现方法,非常不错,具有参考借鉴价值,需要的的朋友参考下
收藏 0 赞 0 分享

C++调用Python基础功能实例详解

c++调用Python首先安装Python,本文以win7为例,给大家详细介绍C++调用Python基础功能,需要的朋友参考下吧
收藏 0 赞 0 分享

C++ 中pragma once 与 #ifndef _XXX_H_ #define _XXX_H_的区别

这篇文章主要介绍了C++ 中pragma once 与 #ifndef _XXX_H_ #define _XXX_H_的区别的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

visual studio 2013中配置opencv图文教程 Opencv2.4.9安装配置教程

这篇文章主要为大家详细介绍了Opencv2.4.9安装教程,以及在visualstudio 2013中opencv的配置步骤,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

VS2013安装配置和使用Boost库教程

这篇文章主要为大家详细介绍了VS2013安装配置和使用Boost库的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

visual studio 2015下boost库配置教程

这篇文章主要为大家详细介绍了visual studio 2015下boost库的配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

VS2010 boost标准库开发环境安装教程

这篇文章主要为大家详细介绍了VS2010 boost标准库开发环境的安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多