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

 更新时间:2019年04月08日 11:25:15   作者:stpeace  
今天小编就为大家分享一篇关于C++中rapidjson组装继续简化的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

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 语言常量

    C 语言基础之初识 C 语言常量

    C语言中的常量分为以下几种:字面常量、const修饰的常变量、#define定义的标识符常量等,下面我们将详细对C语言这几个常量做介绍,感兴趣的小伙伴可以参考一下
    2021-09-09
  • 关于C++静态数据成员的实现讲解

    关于C++静态数据成员的实现讲解

    今天小编就为大家分享一篇关于关于C++静态数据成员的实现讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 基于C++编写一个键盘提示音程序

    基于C++编写一个键盘提示音程序

    首先讲一下思路,这次制作的小黑子相当于键盘提示音,输入J,N,T,M,会发出“鸡你太美”的声音,连续按下JNTM则会发出“你干嘛啊,哎呦”的声音,感兴趣的可以了解一下
    2023-03-03
  • C++基于hook iat改变Messagebox实例

    C++基于hook iat改变Messagebox实例

    这篇文章主要介绍了C++基于hook iat改变Messagebox的方法,以实例形式展示了针对IAT(即导入地址表)以及hook的操作,有助于深入理解Windows程序设计原理,需要的朋友可以参考下
    2014-10-10
  • 浅析C++中的动态内存分配

    浅析C++中的动态内存分配

    这篇文章主要为大家详细介绍了C++中动态内存分配的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • C++实现LeetCode(179.最大组合数)

    C++实现LeetCode(179.最大组合数)

    这篇文章主要介绍了C++实现LeetCode(179.最大组合数),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • 深入学习C语言中的函数指针和左右法则

    深入学习C语言中的函数指针和左右法则

    这篇文章主要介绍了深入学习C语言中的函数指针和左右法则,左右法则是一种常用的C指针声明,需要的朋友可以参考下
    2015-08-08
  • 汇编语言rep movsd 的使用详解

    汇编语言rep movsd 的使用详解

    rep movsd 每次ecx!=0便执行movsd ,然后ecx=ecx-1 movsd移动ds:[si] 到es:[di],在32位汇编下可以用esi代替si,edi代替di
    2013-09-09
  • Qt中PaintEvent绘制实时波形图的实现示例

    Qt中PaintEvent绘制实时波形图的实现示例

    本文主要介绍了Qt中PaintEvent绘制实时波形图的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • C++中字符串与整型及浮点型转换全攻略

    C++中字符串与整型及浮点型转换全攻略

    C++算法刷题等过程中经常会遇到字符串与数字类型的转换,在这其中虽然朴素的算法有不少,但是对于double等类型还是可以说遇到一些麻烦,所以今天就来说说使用C++标准库中的函数实现这些功能。感兴趣的小伙伴一起参与阅读吧
    2021-09-09

最新评论