C++ 如何使用RapidJson 寫入文件
使用RapidJson寫入文件(C++)
本文部分內(nèi)容由AI生成
最初,我希望能夠使用RapidJson 向文件中寫入一個(gè)三級json。其二級json是由for循環(huán)計(jì)算生成的。但是寫來寫去,發(fā)現(xiàn)有很多亂碼,好像是字符串空間在寫入流之前就銷毀的原因?(不確定)于是,使用AI生成了以下例子。
基于C++ 對json文件進(jìn)行寫入
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h> // for prettywriter
#include <rapidjson/filereadstream.h>
#include <rapidjson/filewritestream.h>
#include <cstdio>
using namespace rapidjson;
int main() {
// 創(chuàng)建一個(gè)JSON對象
Document d;
d.SetObject();
Document::AllocatorType& allocator = d.GetAllocator();
d.AddMember("name", Value().SetString("John Doe", allocator), allocator);
d.AddMember("age", 30, allocator);
d.AddMember("is_student", false, allocator);
// 寫入文件
FILE* fp = fopen("example.json", "wb"); // 非Windows平臺可能需要使用 "w"
char writeBuffer[65536];
FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
PrettyWriter<FileWriteStream> writer(os); // 注意,可以不使用PrettyWriter,不過,寫出來的結(jié)構(gòu)不好看,Pretty會將json寫成樹結(jié)構(gòu)
d.Accept(writer);
fclose(fp);
// 讀取文件
fp = fopen("example.json", "rb"); // 非Windows平臺可能需要使用 "r"
char readBuffer[65536];
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d2;
d2.ParseStream(is);
fclose(fp);
// 輸出讀取的內(nèi)容(簡單示例)
printf("Name: %s\n", d2["name"].GetString());
printf("Age: %d\n", d2["age"].GetInt());
printf("Is Student: %s\n", d2["is_student"].GetBool() ? "true" : "false");
return 0;
}使用RapidJson對文件進(jìn)行寫入,寫入的是一個(gè)二級json(包含多個(gè)對象)
#include <cstdio>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h> // for PrettyWriter
#include <rapidjson/filewritestream.h>
using namespace rapidjson;
int main() {
// 創(chuàng)建一個(gè)JSON文檔,這將作為我們的根對象
Document document;
document.SetObject();
Document::AllocatorType& allocator = document.GetAllocator();
// 創(chuàng)建一個(gè)JSON數(shù)組
Value array(kArrayType);
// 創(chuàng)建第一個(gè)對象并添加到數(shù)組
Value object1(kObjectType);
object1.AddMember("id", 1, allocator);
object1.AddMember("name", "John Doe", allocator);
array.PushBack(object1, allocator);
// 創(chuàng)建第二個(gè)對象并添加到數(shù)組
Value object2(kObjectType);
object2.AddMember("id", 2, allocator);
object2.AddMember("name", "Jane Smith", allocator);
array.PushBack(object2, allocator);
// 創(chuàng)建第三個(gè)對象并添加到數(shù)組
Value object3(kObjectType);
object3.AddMember("id", 3, allocator);
object3.AddMember("name", "Alice Johnson", allocator);
array.PushBack(object3, allocator);
// 將數(shù)組添加到根對象
document.AddMember("users", array, allocator);
// 將JSON寫入文件
FILE* fp = fopen("output.json", "wb"); // 非Windows平臺可能需要使用 "w"
char writeBuffer[65536];
FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
PrettyWriter<FileWriteStream> writer(os);
document.Accept(writer);
fclose(fp);
return 0;
}使用RapidJson寫一個(gè)包含多個(gè)對象的二級json文件,使用for循環(huán)
#include <cstdio>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h> // for PrettyWriter
#include <rapidjson/filewritestream.h>
using namespace rapidjson;
int main() {
// 創(chuàng)建一個(gè)JSON文檔,作為根對象
Document document;
document.SetObject();
Document::AllocatorType& allocator = document.GetAllocator();
// 創(chuàng)建一個(gè)JSON數(shù)組
Value usersArray(kArrayType);
// 使用for循環(huán)生成多個(gè)用戶對象
for (int i = 1; i <= 5; ++i) {
Value userObject(kObjectType);
// 為了示例簡單,這里使用i作為id和name的一部分
userObject.AddMember("id", i, allocator);
// 創(chuàng)建一個(gè)具有動態(tài)字符串的Value
std::string name = "User " + std::to_string(i);
Value nameValue;
nameValue.SetString(name.c_str(), static_cast<SizeType>(name.length()), allocator);
userObject.AddMember("name", nameValue, allocator);
// 將用戶對象添加到數(shù)組中
usersArray.PushBack(userObject, allocator);
}
// 將數(shù)組添加到根對象中
document.AddMember("users", usersArray, allocator);
// 寫入文件
FILE* fp = fopen("users_output.json", "wb"); // 非Windows平臺可能需要使用 "w"
char writeBuffer[65536];
FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
PrettyWriter<FileWriteStream> writer(os);
document.Accept(writer);
fclose(fp);
return 0;
}這段代碼首先創(chuàng)建了一個(gè)根JSON文檔document和一個(gè)數(shù)組usersArray。通過for循環(huán),我們創(chuàng)建了五個(gè)用戶對象,每個(gè)對象包含id和name字段,其中name是通過將循環(huán)的索引i轉(zhuǎn)換為字符串并與"User "連接得到的。這些用戶對象被添加到usersArray中,最后這個(gè)數(shù)組被添加到根文檔中,并通過PrettyWriter寫入到名為users_output.json的文件中。
結(jié)果:
{
"users": [
{
"id": 1,
"name": "User 1"
},
{
"id": 2,
"name": "User 2"
},
{
"id": 3,
"name": "User 3"
},
{
"id": 4,
"name": "User 4"
},
{
"id": 5,
"name": "User 5"
}
]
}
使用RapidJson向文件中寫入一個(gè)三級json
#include <cstdio>
#include <string>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/filewritestream.h>
using namespace rapidjson;
int main() {
// 創(chuàng)建根文檔
Document document;
document.SetObject();
Document::AllocatorType& allocator = document.GetAllocator();
// 創(chuàng)建一個(gè)JSON數(shù)組用于存放部門信息
Value departmentsArray(kArrayType);
// 使用for循環(huán)添加部門和員工
for (int depId = 1; depId <= 3; ++depId) {
Value departmentObject(kObjectType);
// 部門ID和名稱
departmentObject.AddMember("departmentId", depId, allocator);
std::string depName = "Department " + std::to_string(depId);
Value depNameValue;
depNameValue.SetString(depName.c_str(), allocator);
departmentObject.AddMember("name", depNameValue, allocator);
// 為每個(gè)部門創(chuàng)建員工數(shù)組
Value employeesArray(kArrayType);
for (int empId = 1; empId <= 4; ++empId) {
Value employeeObject(kObjectType);
employeeObject.AddMember("employeeId", empId, allocator);
std::string empName = "Employee " + std::to_string(empId) + " of Dep " + std::to_string(depId);
Value empNameValue;
empNameValue.SetString(empName.c_str(), allocator);
employeeObject.AddMember("name", empNameValue, allocator);
// 將員工對象添加到員工數(shù)組
employeesArray.PushBack(employeeObject, allocator);
}
// 將員工數(shù)組添加到部門對象
departmentObject.AddMember("employees", employeesArray, allocator);
// 將部門對象添加到部門數(shù)組
departmentsArray.PushBack(departmentObject, allocator);
}
// 將部門數(shù)組添加到根文檔
document.AddMember("departments", departmentsArray, allocator);
// 寫入文件
FILE* fp = fopen("departments_output.json", "wb"); // 非Windows平臺可能需要使用 "w"
char writeBuffer[65536];
FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
PrettyWriter<FileWriteStream> writer(os);
document.Accept(writer);
fclose(fp);
return 0;
}- 我們創(chuàng)建了一個(gè)根文檔document,它包含了一個(gè)名為departments的數(shù)組。
- 通過外層for循環(huán),我們?yōu)槊總€(gè)部門創(chuàng)建了一個(gè)包含基本信息(ID和名稱)的對象,并為每個(gè)部門創(chuàng)建了一個(gè)名為employees的數(shù)組。
- 內(nèi)層for循環(huán)為每個(gè)部門創(chuàng)建了幾個(gè)員工對象,每個(gè)員工對象包含員工的ID和名稱。
- 最后,每個(gè)部門對象(包括其員工數(shù)組)被添加到部門數(shù)組中,整個(gè)部門數(shù)組最終被添加到根文檔中,并通過PrettyWriter寫入到一個(gè)名為departments_output.json的文件中。
結(jié)果
{
"departments": [
{
"departmentId": 1,
"name": "Department 1",
"employees": [
{
"employeeId": 1,
"name": "Employee 1 of Dep 1"
},
{
"employeeId": 2,
"name": "Employee 2 of Dep 1"
},
{
"employeeId": 3,
"name": "Employee 3 of Dep 1"
},
{
"employeeId": 4,
"name": "Employee 4 of Dep 1"
}
]
},
{
"departmentId": 2,
"name": "Department 2",
"employees": [
{
"employeeId": 1,
"name": "Employee 1 of Dep 2"
},
{
"employeeId": 2,
"name": "Employee 2 of Dep 2"
},
{
"employeeId": 3,
"name": "Employee 3 of Dep 2"
},
{
"employeeId": 4,
"name": "Employee 4 of Dep 2"
}
]
},
{
"departmentId": 3,
"name": "Department 3",
"employees": [
{
"employeeId": 1,
"name": "Employee 1 of Dep 3"
},
{
"employeeId": 2,
"name": "Employee 2 of Dep 3"
},
{
"employeeId": 3,
"name": "Employee 3 of Dep 3"
},
{
"employeeId": 4,
"name": "Employee 4 of Dep 3"
}
]
}
]
}
到此這篇關(guān)于C++ 使用RapidJson 寫入文件的文章就介紹到這了,更多相關(guān)C++ 寫入文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中typedef的用法以及#define區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于C語言中typedef用法以及#define區(qū)別的相關(guān)資料,typedef 是用來定義一種類型的新別名的,它不同于宏(#define),不是簡單的字符串替換。而#define只是簡單的字符串替換(原地?cái)U(kuò)展),需要的朋友可以參考下2021-07-07
Cocos2d-x學(xué)習(xí)筆記之Hello World源碼分析
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之Hello World源碼分析,接上一篇內(nèi)容,本文著重分析源碼文件,需要的朋友可以參考下2014-09-09
C++中auto類型說明符詳解(附易錯(cuò)實(shí)例)
這篇文章主要給大家介紹了關(guān)于C++中auto類型說明符的相關(guān)資料,文中還附易錯(cuò)實(shí)例,在C++11中引入了auto類型說明符,用它就能讓編譯器替我們?nèi)シ治霰磉_(dá)式所屬的類型,需要的朋友可以參考下2023-07-07
C++入門基礎(chǔ)之命名空間、輸入輸出和缺省參數(shù)
C++入門基礎(chǔ)篇的內(nèi)容為C++的基本特性,只有在掌握C++的基本特性后,是進(jìn)入后面類和對象學(xué)習(xí)的基礎(chǔ),下面這篇文章主要給大家介紹了關(guān)于C++入門基礎(chǔ)之命名空間、輸入輸出和缺省參數(shù)的相關(guān)資料,需要的朋友可以參考下2023-01-01
Qt6遠(yuǎn)程連接MySQL數(shù)據(jù)庫的簡單易上手版
在Qt應(yīng)用程序里,可實(shí)現(xiàn)遠(yuǎn)程MySQL服務(wù)器的連接操作,本文就來介紹一下Qt6遠(yuǎn)程連接MySQL數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11

