腾讯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;
}
相关推荐
面朝大海,春不暖,花不开10 小时前
结构化数据格式解析:JSON 与 XML 的技术应用与实践
xml·json
PythonicCC13 小时前
Django核心知识点详解:JSON、AJAX、Cookie、Session与用户认证
ajax·django·json
-凌凌漆-15 小时前
【Qt】Qt QML json处理
开发语言·qt·json
racerun1 天前
UniApp中的pages.json 和 tabBar
uni-app·json
我命由我123451 天前
前端开发问题:SyntaxError: “undefined“ is not valid JSON
开发语言·前端·javascript·vue.js·json·ecmascript·js
Code季风2 天前
将 gRPC 服务注册到 Consul:从配置到服务发现的完整实践(上)
数据库·微服务·go·json·服务发现·consul
yjb.gz2 天前
Oracle函数JSON_TABLE使用
数据库·oracle·json
百锦再4 天前
.Net配置文件appsetting.json的几种读取方法
chrome·json·.net·依赖注入·appsetting·web.config
葬歌倾城5 天前
JSON的缩进格式方式和紧凑格式方式
c#·json
火车叨位去19496 天前
Java中的JSON序列化和反序列化
json