当前位置: 首页 > article >正文

腾讯rapidJson使用例子

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

#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;
}


http://www.kler.cn/a/419916.html

相关文章:

  • 我与Linux的爱恋:共享内存
  • 【新人系列】Python 入门(十五):异常类型
  • Java 8 Stream API 入门教程:轻松使用 map、filter 和 collect 进行数据处理
  • PyCharm中Python项目打包并运行到服务器的简明指南
  • SpringBoot3 + Vue3 由浅入深的交互 基础交互教学2
  • 数据库管理-第267期 23ai:Oracle Data Redaction演示(20241128)
  • CSS学习记录03
  • NLP 的发展历程
  • 洛谷 P1308 [NOIP2011 普及组] 统计单词数 C语言
  • 对于大规模的淘宝API接口数据,有什么高效的处理方法?
  • 2. langgraph中的Tool Calling (How to handle tool calling errors)
  • AI在SEO中的应用与关键词优化探讨
  • 011变长子网掩码
  • React 前端框架1
  • <<WTF-Solidity>>学习笔记(part 25-28)
  • 物联网——WatchDog(监听器)
  • 关于正点原子STM32F404探索者V3ZGT6 RS232串口的使用
  • navicat连接mysql 8.0以上版本2059错误
  • Gazebo插件相机传感器(可订阅/camera/image_raw话题)
  • OpenCV_Code_LOG