腾讯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;
}
相关推荐
会飞的鱼先生5 小时前
vue2、vue3项目打包生成txt文件-自动记录打包日期:git版本、当前分支、提交人姓名、提交日期、提交描述等信息 和 前端项目的版本号json文件
前端·vue.js·git·json
朴拙数科1 天前
Json rpc 2.0比起传统Json在通信中的优势
网络协议·rpc·json
LYPHARD MELODY。2 天前
将 JSON 批量转换为 XML:深度解析与完整实现指南
xml·json
爱吃涮毛肚的肥肥(暂时吃不了版)2 天前
项目班——0510——JSON网络封装
c++·算法·json
GISer_Jing2 天前
[前端高频]数组转树、数组扁平化、深拷贝、JSON.stringify&JSON.parse等手撕
前端·javascript·json
小吕学编程3 天前
Jackson使用详解
java·javascript·数据库·json
双叶8363 天前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)(网页版预告)(html)(js)(json)
c语言·javascript·数据结构·html·json
电商数据girl3 天前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
bing_1584 天前
Spring MVC 根据请求头 (如 Accept) 怎么返回 JSON 或 XML 数据?
spring·json·mvc
半路_出家ren5 天前
python处理异常,JSON
python·json·异常处理