C++ 如何使用RapidJson 寫入文件
使用RapidJson寫入文件(C++)
本文部分內容由AI生成
最初,我希望能夠使用RapidJson 向文件中寫入一個三級json。其二級json是由for循環(huán)計算生成的。但是寫來寫去,發(fā)現(xiàn)有很多亂碼,好像是字符串空間在寫入流之前就銷毀的原因?(不確定)于是,使用AI生成了以下例子。
基于C++ 對json文件進行寫入
#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)建一個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,不過,寫出來的結構不好看,Pretty會將json寫成樹結構 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); // 輸出讀取的內容(簡單示例) 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對文件進行寫入,寫入的是一個二級json(包含多個對象)
#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)建一個JSON文檔,這將作為我們的根對象 Document document; document.SetObject(); Document::AllocatorType& allocator = document.GetAllocator(); // 創(chuàng)建一個JSON數(shù)組 Value array(kArrayType); // 創(chuàng)建第一個對象并添加到數(shù)組 Value object1(kObjectType); object1.AddMember("id", 1, allocator); object1.AddMember("name", "John Doe", allocator); array.PushBack(object1, allocator); // 創(chuàng)建第二個對象并添加到數(shù)組 Value object2(kObjectType); object2.AddMember("id", 2, allocator); object2.AddMember("name", "Jane Smith", allocator); array.PushBack(object2, allocator); // 創(chuàng)建第三個對象并添加到數(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寫一個包含多個對象的二級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)建一個JSON文檔,作為根對象 Document document; document.SetObject(); Document::AllocatorType& allocator = document.GetAllocator(); // 創(chuàng)建一個JSON數(shù)組 Value usersArray(kArrayType); // 使用for循環(huán)生成多個用戶對象 for (int i = 1; i <= 5; ++i) { Value userObject(kObjectType); // 為了示例簡單,這里使用i作為id和name的一部分 userObject.AddMember("id", i, allocator); // 創(chuàng)建一個具有動態(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)建了一個根JSON文檔document
和一個數(shù)組usersArray
。通過for
循環(huán),我們創(chuàng)建了五個用戶對象,每個對象包含id
和name
字段,其中name
是通過將循環(huán)的索引i
轉換為字符串并與"User "
連接得到的。這些用戶對象被添加到usersArray
中,最后這個數(shù)組被添加到根文檔中,并通過PrettyWriter
寫入到名為users_output.json
的文件中。
結果:
{
"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向文件中寫入一個三級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)建一個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); // 為每個部門創(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)建了一個根文檔document,它包含了一個名為departments的數(shù)組。
- 通過外層for循環(huán),我們?yōu)槊總€部門創(chuàng)建了一個包含基本信息(ID和名稱)的對象,并為每個部門創(chuàng)建了一個名為employees的數(shù)組。
- 內層for循環(huán)為每個部門創(chuàng)建了幾個員工對象,每個員工對象包含員工的ID和名稱。
- 最后,每個部門對象(包括其員工數(shù)組)被添加到部門數(shù)組中,整個部門數(shù)組最終被添加到根文檔中,并通過PrettyWriter寫入到一個名為departments_output.json的文件中。
結果
{
"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"
}
]
}
]
}
到此這篇關于C++ 使用RapidJson 寫入文件的文章就介紹到這了,更多相關C++ 寫入文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言中typedef的用法以及#define區(qū)別詳解
這篇文章主要給大家介紹了關于C語言中typedef用法以及#define區(qū)別的相關資料,typedef 是用來定義一種類型的新別名的,它不同于宏(#define),不是簡單的字符串替換。而#define只是簡單的字符串替換(原地擴展),需要的朋友可以參考下2021-07-07Qt6遠程連接MySQL數(shù)據(jù)庫的簡單易上手版
在Qt應用程序里,可實現(xiàn)遠程MySQL服務器的連接操作,本文就來介紹一下Qt6遠程連接MySQL數(shù)據(jù)庫,具有一定的參考價值,感興趣的可以了解一下2023-11-11