C++中rapidjson組裝繼續(xù)簡化的方法
rapidjson組裝繼續(xù)簡化------人生苦短,我用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;
}
結(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>
// 請自己下載開源的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"}]}
再來看一個:
#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;
}
結(jié)果:
{"oh1":[{"age":30,"name":"taoge"}],"oh2":"hehe"}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
關(guān)于C++靜態(tài)數(shù)據(jù)成員的實現(xiàn)講解
今天小編就為大家分享一篇關(guān)于關(guān)于C++靜態(tài)數(shù)據(jù)成員的實現(xiàn)講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
C++實現(xiàn)LeetCode(179.最大組合數(shù))
這篇文章主要介紹了C++實現(xiàn)LeetCode(179.最大組合數(shù)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
Qt中PaintEvent繪制實時波形圖的實現(xiàn)示例
本文主要介紹了Qt中PaintEvent繪制實時波形圖的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06

