腾讯rapidJson使用例子

只需要把库的头文件拿下来加入项目中使用就行,我是以二进制文件存储内容并解析:

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
#include·<rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/error/en.h>

void writeJsonToBinaryFile(const std::string& jsonString, const std::string& filename) {
    std::ofstream outFile(filename, std::ios::binary);
    if (!outFile) {
        std::cerr << "无法打开文件进行写入: " << filename << std::endl;
        return;
    }
    
    // 将 JSON 字符串写入文件
    outFile.write(jsonString.c_str(), jsonString.size());
    outFile.close();
}

std::string readBinaryFileToJsonString(const std::string& filename) {
    std::ifstream inFile(filename, std::ios::binary);
    if (!inFile) {
        std::cerr << "无法打开文件进行读取: " << filename << std::endl;
        return "";
    }

    // 读取文件内容到字符串
    std::string jsonString((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
    inFile.close();
    return jsonString;
}

int main() {

    // 创建 JSON 字符串
    rapidjson::Document document;
    document.SetObject();
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
    document.AddMember("name", rapidjson::Value("John", allocator), allocator);
    document.AddMember("age", 30, allocator);
    document.AddMember("isStudent", false, allocator);

    // 将 JSON 对象转换为字符串
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    document.Accept(writer);
    std::string jsonString = buffer.GetString();

    // 写入二进制文件
    writeJsonToBinaryFile(jsonString, "data.bin");
	
    // 从二进制文件读取 JSON 字符串
    std::string jsonString = readBinaryFileToJsonString("data.bin");
    if (jsonString.empty()) {
        return 1; // 读取失败
    }

    // 解析 JSON 字符串
    rapidjson::Document document2;
    if (document2.Parse(jsonString.c_str()).HasParseError()) {
        std::cerr << "JSON 解析错误: " << rapidjson::GetParseError_En(document2.GetParseError()) << std::endl;
        return 1;
    }

    // 访问 JSON 数据
    const char* name = document2["name"].GetString();
    int age = document2["age"].GetInt();
    bool isStudent = document2["isStudent"].GetBool();

    // 输出解析结果
    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Is Student: " << (isStudent ? "true" : "false") << std::endl;

    return 0;
}
相关推荐
Mr.小怪9 小时前
【开源】开发了一个在终端里运行的轻量级Excel:可以查看、简单编辑、转JSON
json
CHQIUU19 小时前
在 C# .NET 中驾驭 JSON:使用 Newtonsoft.Json 进行解析与 POST 请求实战
c#·json·.net
i_am_a_div_日积月累_1 天前
git检查提交分支和package.json的version版本是否一致
git·json
Java Fans1 天前
WPF使用SQLite与JSON文本文件结合存储体侧平衡数据的设计与实现
sqlite·json·wpf
还不如ctrC+V1 天前
VScode在 Markdown 编辑器中预览
node.js·json
一个天蝎座 白勺 程序猿2 天前
Python爬虫(8)Python数据存储实战:JSON文件读写与复杂结构化数据处理指南
爬虫·python·json
一路向北he2 天前
pcm数据不支持存储在json里面,需要先转base64
json·pcm
沉迷...4 天前
详解.vscode 下的json .vscode文件夹下各个文件的作用
ide·vscode·json
聪明的墨菲特i4 天前
SQL进阶知识:九、高级数据类型
xml·数据库·sql·mysql·json·空间数据类型
AAA顶置摸鱼4 天前
使用 Pandas 进行多格式数据整合:从 Excel、JSON 到 HTML 的处理实战
json·excel·pandas