亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++ 如何使用RapidJson 寫入文件

 更新時間:2024年04月01日 10:38:33   作者:SUNX-T  
RapidJSON 是只有頭文件的 C++ 庫, 不需要編譯, 可以直接在項目中使用, 只需把 include/rapidjson 目錄復制至系統(tǒng)或項目的 include 目錄即可,下面給大家分享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)建了五個用戶對象,每個對象包含idname字段,其中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ū)別詳解

    這篇文章主要給大家介紹了關于C語言中typedef用法以及#define區(qū)別的相關資料,typedef 是用來定義一種類型的新別名的,它不同于宏(#define),不是簡單的字符串替換。而#define只是簡單的字符串替換(原地擴展),需要的朋友可以參考下
    2021-07-07
  • Cocos2d-x學習筆記之Hello World源碼分析

    Cocos2d-x學習筆記之Hello World源碼分析

    這篇文章主要介紹了Cocos2d-x學習筆記之Hello World源碼分析,接上一篇內容,本文著重分析源碼文件,需要的朋友可以參考下
    2014-09-09
  • Qt 儀表盤的實現(xiàn)示例

    Qt 儀表盤的實現(xiàn)示例

    儀表盤在很多汽車和物聯(lián)網(wǎng)相關的系統(tǒng)中很常用,本文就來介紹一下Qt 儀表盤的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • C++中auto類型說明符詳解(附易錯實例)

    C++中auto類型說明符詳解(附易錯實例)

    這篇文章主要給大家介紹了關于C++中auto類型說明符的相關資料,文中還附易錯實例,在C++11中引入了auto類型說明符,用它就能讓編譯器替我們去分析表達式所屬的類型,需要的朋友可以參考下
    2023-07-07
  • C++ 前置聲明詳解及實例

    C++ 前置聲明詳解及實例

    這篇文章主要介紹了C++ 前置聲明詳解及實例的相關資料,需要的朋友可以參考下
    2017-06-06
  • C++ 超全面講解多態(tài)

    C++ 超全面講解多態(tài)

    這篇文章主要介紹了C++多態(tài)的原理與實現(xiàn),多態(tài)是一種面向對象的設計思路,本身和C++不是強綁定的,其他語言當中一樣有多態(tài),只不過實現(xiàn)的方式可能有所不同。下面來一起了解更多詳細內容吧
    2022-04-04
  • Matlab繪制雨云圖的方法詳解

    Matlab繪制雨云圖的方法詳解

    這篇文章主要介紹了如何利用Matlab實現(xiàn)雨云圖的繪制,文中的示例代碼講解詳細,對我們學習Matlab有一定的幫助,需要的可以參考一下
    2022-05-05
  • C++入門基礎之命名空間、輸入輸出和缺省參數(shù)

    C++入門基礎之命名空間、輸入輸出和缺省參數(shù)

    C++入門基礎篇的內容為C++的基本特性,只有在掌握C++的基本特性后,是進入后面類和對象學習的基礎,下面這篇文章主要給大家介紹了關于C++入門基礎之命名空間、輸入輸出和缺省參數(shù)的相關資料,需要的朋友可以參考下
    2023-01-01
  • 深入理解C++編程中的局部變量和全局變量

    深入理解C++編程中的局部變量和全局變量

    這篇文章主要介紹了深入理解C++編程中的局部變量和全局變量,是C++入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • Qt6遠程連接MySQL數(shù)據(jù)庫的簡單易上手版

    Qt6遠程連接MySQL數(shù)據(jù)庫的簡單易上手版

    在Qt應用程序里,可實現(xiàn)遠程MySQL服務器的連接操作,本文就來介紹一下Qt6遠程連接MySQL數(shù)據(jù)庫,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11

最新評論