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

所属分类: 软件编程 / C 语言 阅读数: 48
收藏 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之旅开始了)[三]

C 语言基础教程(我的C之旅开始了)[三]
收藏 0 赞 0 分享

C 语言基础教程(我的C之旅开始了)[四]

C 语言基础教程(我的C之旅开始了)[四]
收藏 0 赞 0 分享

C 语言基础教程(我的C之旅开始了)[五]

C 语言基础教程(我的C之旅开始了)[五]
收藏 0 赞 0 分享

C 语言基础教程(我的C之旅开始了)[六]

C 语言基础教程(我的C之旅开始了)[六]
收藏 0 赞 0 分享

C 语言基础教程(我的C之旅开始了)[七]

C 语言基础教程(我的C之旅开始了)[七]
收藏 0 赞 0 分享

C 语言基础教程(我的C之旅开始了)[八]

C 语言基础教程(我的C之旅开始了)[八]
收藏 0 赞 0 分享

C 语言基础教程(我的C之旅开始了)[九]

C 语言基础教程(我的C之旅开始了)[九]
收藏 0 赞 0 分享

C 语言基础教程(我的C之旅开始了)[十]

C 语言基础教程(我的C之旅开始了)[十]
收藏 0 赞 0 分享

tc编译的dos程序和vc编译的win32控制台程序的异同

tc编译的dos程序和vc编译的win32控制台程序的异同
收藏 0 赞 0 分享

C语言WinSock学习笔记第1/2页

本篇文章主要介绍了C语言WinSock学习笔记,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多