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

所属分类: 软件编程 / C 语言 阅读数: 135
收藏 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++指针数组、数组指针、数组名及二维数组技巧汇总,对于深入理解C++数组与指针来说非常重要,需要的朋友可以参考下
收藏 0 赞 0 分享

C++类中的static和const用法实例教程

这篇文章主要介绍了C++类中的static和const用法,是C++面向对象程序设计中非常重要的概念,需要的朋友可以参考下
收藏 0 赞 0 分享

C++中基类和派生类之间的转换实例教程

这篇文章主要介绍了C++中基类和派生类之间的转换,有助于深入理解C++面向对象程序设计,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++中内存对齐实例教程

这篇文章主要介绍了VC++中内存对齐的实现方法,具有很高的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

深入理解C++中public、protected及private用法

这篇文章主要介绍了C++中public、protected及private用法,对于C++面向对象程序设计来说是非常重要的概念,需要的朋友可以参考下
收藏 0 赞 0 分享

C++中重载、重写(覆盖)和隐藏的区别实例分析

这篇文章主要介绍了C++中重载、重写(覆盖)和隐藏的区别,是C++面向对象程序设计非常重要的概念,需要的朋友可以参考下
收藏 0 赞 0 分享

MFC控件之CListCtrl的应用实例教程

这篇文章主要介绍了MFC控件中CListCtrl的应用方法,包括了针对表格的一些操作,是MFC中比较重要的一个控件类,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言变量类型与输出控制用法实例教程

这篇文章主要介绍了C语言变量类型与输出控制用法,是C语言程序设计中比较基础也是比较重要的用法,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言循环结构与时间函数用法实例教程

这篇文章主要介绍了C语言循环结构与时间函数用法,是C语言中非常重要的一个技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多