最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2024年04月01日 10:38:33   作者:SUNX-T  
RapidJSON 是只有頭文件的 C++ 庫(kù), 不需要編譯, 可以直接在項(xiàng)目中使用, 只需把 include/rapidjson 目錄復(fù)制至系統(tǒng)或項(xiàng)目的 include 目錄即可,下面給大家分享C++ 如何使用RapidJson 寫入文件,感興趣的朋友跟隨小編一起看看吧

使用RapidJson寫入文件(C++)

本文部分內(nèi)容由AI生成

最初,我希望能夠使用RapidJson 向文件中寫入一個(gè)三級(jí)json。其二級(jí)json是由for循環(huán)計(jì)算生成的。但是寫來寫去,發(fā)現(xiàn)有很多亂碼,好像是字符串空間在寫入流之前就銷毀的原因?(不確定)于是,使用AI生成了以下例子。

基于C++ 對(duì)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對(duì)象
    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平臺(tái)可能需要使用 "w"
    char writeBuffer[65536];
    FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
    PrettyWriter<FileWriteStream> writer(os);   // 注意,可以不使用PrettyWriter,不過,寫出來的結(jié)構(gòu)不好看,Pretty會(huì)將json寫成樹結(jié)構(gòu)
    d.Accept(writer);
    fclose(fp);
    // 讀取文件
    fp = fopen("example.json", "rb"); // 非Windows平臺(tái)可能需要使用 "r"
    char readBuffer[65536];
    FileReadStream is(fp, readBuffer, sizeof(readBuffer));
    Document d2;
    d2.ParseStream(is);
    fclose(fp);
    // 輸出讀取的內(nèi)容(簡(jiǎn)單示例)
    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對(duì)文件進(jìn)行寫入,寫入的是一個(gè)二級(jí)json(包含多個(gè)對(duì)象)

#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文檔,這將作為我們的根對(duì)象
    Document document;
    document.SetObject();
    Document::AllocatorType& allocator = document.GetAllocator();
    // 創(chuàng)建一個(gè)JSON數(shù)組
    Value array(kArrayType);
    // 創(chuàng)建第一個(gè)對(duì)象并添加到數(shù)組
    Value object1(kObjectType);
    object1.AddMember("id", 1, allocator);
    object1.AddMember("name", "John Doe", allocator);
    array.PushBack(object1, allocator);
    // 創(chuàng)建第二個(gè)對(duì)象并添加到數(shù)組
    Value object2(kObjectType);
    object2.AddMember("id", 2, allocator);
    object2.AddMember("name", "Jane Smith", allocator);
    array.PushBack(object2, allocator);
    // 創(chuàng)建第三個(gè)對(duì)象并添加到數(shù)組
    Value object3(kObjectType);
    object3.AddMember("id", 3, allocator);
    object3.AddMember("name", "Alice Johnson", allocator);
    array.PushBack(object3, allocator);
    // 將數(shù)組添加到根對(duì)象
    document.AddMember("users", array, allocator);
    // 將JSON寫入文件
    FILE* fp = fopen("output.json", "wb"); // 非Windows平臺(tái)可能需要使用 "w"
    char writeBuffer[65536];
    FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
    PrettyWriter<FileWriteStream> writer(os);
    document.Accept(writer);
    fclose(fp);
    return 0;
}

使用RapidJson寫一個(gè)包含多個(gè)對(duì)象的二級(jí)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文檔,作為根對(duì)象
    Document document;
    document.SetObject();
    Document::AllocatorType& allocator = document.GetAllocator();
    // 創(chuàng)建一個(gè)JSON數(shù)組
    Value usersArray(kArrayType);
    // 使用for循環(huán)生成多個(gè)用戶對(duì)象
    for (int i = 1; i <= 5; ++i) {
        Value userObject(kObjectType);
        // 為了示例簡(jiǎn)單,這里使用i作為id和name的一部分
        userObject.AddMember("id", i, allocator);
        // 創(chuàng)建一個(gè)具有動(dò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);
        // 將用戶對(duì)象添加到數(shù)組中
        usersArray.PushBack(userObject, allocator);
    }
    // 將數(shù)組添加到根對(duì)象中
    document.AddMember("users", usersArray, allocator);
    // 寫入文件
    FILE* fp = fopen("users_output.json", "wb"); // 非Windows平臺(tái)可能需要使用 "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è)用戶對(duì)象,每個(gè)對(duì)象包含idname字段,其中name是通過將循環(huán)的索引i轉(zhuǎn)換為字符串并與"User "連接得到的。這些用戶對(duì)象被添加到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è)三級(jí)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);
            // 將員工對(duì)象添加到員工數(shù)組
            employeesArray.PushBack(employeeObject, allocator);
        }
        // 將員工數(shù)組添加到部門對(duì)象
        departmentObject.AddMember("employees", employeesArray, allocator);
        // 將部門對(duì)象添加到部門數(shù)組
        departmentsArray.PushBack(departmentObject, allocator);
    }
    // 將部門數(shù)組添加到根文檔
    document.AddMember("departments", departmentsArray, allocator);
    // 寫入文件
    FILE* fp = fopen("departments_output.json", "wb"); // 非Windows平臺(tái)可能需要使用 "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和名稱)的對(duì)象,并為每個(gè)部門創(chuàng)建了一個(gè)名為employees的數(shù)組。
  • 內(nèi)層for循環(huán)為每個(gè)部門創(chuàng)建了幾個(gè)員工對(duì)象,每個(gè)員工對(duì)象包含員工的ID和名稱。
  • 最后,每個(gè)部門對(duì)象(包括其員工數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入分析C語(yǔ)言存儲(chǔ)類型與用戶空間內(nèi)部分布

    深入分析C語(yǔ)言存儲(chǔ)類型與用戶空間內(nèi)部分布

    這篇文章主要介紹了C語(yǔ)言存儲(chǔ)類型與用戶空間內(nèi)部分布,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12
  • Opencv透視變換綜合實(shí)例詳解

    Opencv透視變換綜合實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了Opencv透視變換綜合實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C++如何保存bmp圖片

    C++如何保存bmp圖片

    這篇文章主要介紹了C++如何保存bmp圖片問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 使用C++的ORM框架QxORM詳解

    使用C++的ORM框架QxORM詳解

    這篇文章主要介紹了使用C++的ORM框架QxORM的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Qt股票組件之自選股列表拖拽、右鍵常用菜單功能的實(shí)現(xiàn)

    Qt股票組件之自選股列表拖拽、右鍵常用菜單功能的實(shí)現(xiàn)

    這篇文章主要介紹了Qt股票組件之自選股列表拖拽、右鍵常用菜單功能的實(shí)現(xiàn)方法,本文通過實(shí)例文字相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 深入解析C語(yǔ)言中的內(nèi)存分配相關(guān)問題

    深入解析C語(yǔ)言中的內(nèi)存分配相關(guān)問題

    這篇文章主要深入地介紹了C語(yǔ)言中的內(nèi)存分配,C語(yǔ)言編程中的內(nèi)存泄漏問題一直以來都是C編程中的一大棘手問題,本文從malloc和指針等方面對(duì)C內(nèi)存進(jìn)行了深層次講解,強(qiáng)烈推薦!需要的朋友可以參考下
    2015-08-08
  • 基于C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲

    基于C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲

    這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++ 日志庫(kù)log4cpp使用詳解

    C++ 日志庫(kù)log4cpp使用詳解

    log4cpp是基于log4j設(shè)計(jì)的開源日志庫(kù),包括日志級(jí)別控制、多種輸出目的地、日志格式自定義等,本文介紹了C++日志庫(kù)log4cpp的核心概念與使用方法,感興趣的可以了解一下
    2026-01-01
  • 一文帶你了解C++中deque的使用

    一文帶你了解C++中deque的使用

    C++中的deque是一種雙端隊(duì)列,可以在隊(duì)列的前端和后端進(jìn)行插入元素和刪除操作,同時(shí)可以視作一個(gè)長(zhǎng)度不定的數(shù)組,支持高效的插入和刪除操作。本篇文章將深入探討C++中的deque的使用,感興趣的可以了解一下
    2023-05-05
  • C++輸出問題:保留兩位小數(shù)

    C++輸出問題:保留兩位小數(shù)

    這篇文章主要介紹了C++輸出問題:保留兩位小數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評(píng)論

沿河| 武川县| 依安县| 新绛县| 卓尼县| 京山县| 凤阳县| 桃源县| 咸丰县| 罗江县| 苍溪县| 修水县| 荥阳市| 新郑市| 徐水县| 温宿县| 英超| 环江| 武川县| 定结县| 儋州市| 泰顺县| 广河县| 泰州市| 图片| 荆门市| 德钦县| 沂南县| 观塘区| 汝州市| 兰州市| 绥滨县| 三门县| 冕宁县| 个旧市| 淮北市| 清流县| 龙山县| 怀集县| 丹巴县| 襄樊市|