腾讯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;
}
相关推荐
_风中无我。16 小时前
Spring的过滤器获取请求体中JSON参数,同时解决Controller获取不到请求体参数的问题。
java·spring·json
whisperrr.20 小时前
【JavaWeb12】数据交换与异步请求:JSON与Ajax的绝妙搭配是否塑造了Web的交互革命?
前端·ajax·json
子非衣1 天前
MySQL修改JSON格式数据示例
android·mysql·json
林的快手3 天前
伪类选择器
android·前端·css·chrome·ajax·html·json
css趣多多4 天前
初步安装和使用vant组件库,使用css变量定制vant主题样式 ,小程序的API Promise化,调用promise化之API
json
Web极客码4 天前
WordPress“更新失败,响应不是有效的JSON响应”问题的修复
json·github·wordpress
gywl6 天前
Spring Web MVC入门
spring·json·mvc·注解·cookie·session
运维小文7 天前
Mongodb数据管理
数据库·mongodb·json·非关系数据库·文档型数据库
玉阳软件yuyangdev_cn7 天前
铁塔电单车协议对接电单车TCP json协议对接成熟充电桩系统搭建低速充电桩TCP 接口规范
网络·tcp/ip·json·铁塔协议
m0_748232928 天前
SpringCloud系列教程:微服务的未来 (五)枚举处理器、JSON处理器、分页插件实现
spring cloud·微服务·json