C++中rapidjson組裝繼續(xù)簡(jiǎn)化的方法
rapidjson組裝繼續(xù)簡(jiǎn)化------人生苦短,我用rapidjson
看最簡(jiǎn)單的:
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 請(qǐng)自己下載開(kāi)源的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;
}
結(jié)果:
{"age":29,"name":"taoge"}
再看數(shù)組:
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 請(qǐng)自己下載開(kāi)源的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;
}
結(jié)果:
{"json":[{"age":30,"name":"taoge"}]}
再來(lái)看一個(gè):
#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 請(qǐng)自己下載開(kāi)源的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;
}
結(jié)果:
{"oh1":[{"age":30,"name":"taoge"}],"oh2":"hehe"}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C 語(yǔ)言基礎(chǔ)之初識(shí) C 語(yǔ)言常量
C語(yǔ)言中的常量分為以下幾種:字面常量、const修飾的常變量、#define定義的標(biāo)識(shí)符常量等,下面我們將詳細(xì)對(duì)C語(yǔ)言這幾個(gè)常量做介紹,感興趣的小伙伴可以參考一下2021-09-09
關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解
今天小編就為大家分享一篇關(guān)于關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
C++基于hook iat改變Messagebox實(shí)例
這篇文章主要介紹了C++基于hook iat改變Messagebox的方法,以實(shí)例形式展示了針對(duì)IAT(即導(dǎo)入地址表)以及hook的操作,有助于深入理解Windows程序設(shè)計(jì)原理,需要的朋友可以參考下2014-10-10
C++實(shí)現(xiàn)LeetCode(179.最大組合數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(179.最大組合數(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
深入學(xué)習(xí)C語(yǔ)言中的函數(shù)指針和左右法則
這篇文章主要介紹了深入學(xué)習(xí)C語(yǔ)言中的函數(shù)指針和左右法則,左右法則是一種常用的C指針聲明,需要的朋友可以參考下2015-08-08
Qt中PaintEvent繪制實(shí)時(shí)波形圖的實(shí)現(xiàn)示例
本文主要介紹了Qt中PaintEvent繪制實(shí)時(shí)波形圖的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C++中字符串與整型及浮點(diǎn)型轉(zhuǎn)換全攻略
C++算法刷題等過(guò)程中經(jīng)常會(huì)遇到字符串與數(shù)字類型的轉(zhuǎn)換,在這其中雖然樸素的算法有不少,但是對(duì)于double等類型還是可以說(shuō)遇到一些麻煩,所以今天就來(lái)說(shuō)說(shuō)使用C++標(biāo)準(zhǔn)庫(kù)中的函數(shù)實(shí)現(xiàn)這些功能。感興趣的小伙伴一起參與閱讀吧2021-09-09

