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"}]}
再來看一個(gè):
#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)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
VC6.0如何創(chuàng)建以及調(diào)用動態(tài)鏈接庫實(shí)例詳解
作為客戶與后臺的中介,為了更好的調(diào)節(jié)兩方的關(guān)系,我明智滴選擇了webservice以及動態(tài)鏈接庫。在與客戶c++使動態(tài)鏈接庫方式,而與后臺java,使用webservice來交流溝通2013-01-01
C++深入淺出探索數(shù)據(jù)結(jié)構(gòu)的原理
C++的數(shù)據(jù)結(jié)構(gòu)很多,很復(fù)雜,所以本文將通過示例帶大家深入了解一下C++中的數(shù)據(jù)結(jié)構(gòu)與算法。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05
C++ STL入門教程(2) list雙向鏈表使用方法(附程序代碼)
這篇文章主要為大家詳細(xì)介紹了C++ STL入門教程第二篇,list雙向鏈表使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
C語言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)飛機(jī)訂票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12

