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

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

本篇文章是对全排列算法的非递归实现与递归实现的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入N皇后问题的两个最高效算法的详解

本篇文章是对N皇后问题的两个最高效的算法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

fatal error LNK1104: 无法打开文件“libc.lib”的解决方法

本篇文章是对fatal error LNK1104: 无法打开文件“libc.lib”的解决方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

数组中求第K大数的实现方法

本篇文章是对数组中求第K大数的实现方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入第K大数问题以及算法概要的详解

本篇文章是对第K大数问题以及算法概要进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

如何寻找数组中的第二大数

本篇文章是对如何寻找数组中的第二大数进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

用C++实现DBSCAN聚类算法

本篇文章是对使用C++实现DBSCAN聚类算法的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

大数(高精度数)模板(分享)

本篇文章对大数(高精度数)模板进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入理解大数与高精度数的处理问题

本篇文章是对大数与高精度数的处理进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

C++大数模板(推荐)

本篇文章是对C++大数模板的程序代码进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多