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

网站建设实用教程海外永久网站

网站建设实用教程,海外永久网站,网站福利你们会回来感谢我的,c语言做的网站😏★,:.☆( ̄▽ ̄)/$:.★ 😏 这篇文章主要介绍rapidjson数据解析库配置与使用。 无专精则不能成,无涉猎则不能通。——梁启超 欢迎来到我的博客,一起学习,共同进步。 喜欢的朋友可以关注一下&…

😏★,°:.☆( ̄▽ ̄)/$:.°★ 😏
这篇文章主要介绍rapidjson数据解析库配置与使用。
无专精则不能成,无涉猎则不能通。——梁启超
欢迎来到我的博客,一起学习,共同进步。
喜欢的朋友可以关注一下,下次更新不迷路🥞

文章目录

    • :smirk:1. 项目介绍
    • :blush:2. 环境配置
    • :satisfied:3. 使用说明
      • 解析json数据示例:
      • 写入json数据示例:
      • 从文件中解析json
      • 将json数据写入文件

😏1. 项目介绍

项目Github地址:https://github.com/Tencent/rapidjson

RapidJSON 是一个快速的 C++ JSON 解析器/生成器,具有高效的内存利用和低延迟。它是一个轻量级的、模块化的、功能齐全的 JSON 库,广泛应用于 C++ 程序中用于处理 JSON 数据。

RapidJSON 的特点包括:

1.快速高效:RapidJSON 通过最大程度地优化内存使用和计算效率来实现快速的 JSON 解析和生成,它在性能上表现出色。

2.标准兼容:RapidJSON 完全符合 JSON 标准(RFC 8259),可以处理各种合法的 JSON 数据。

3.模块化设计:RapidJSON 的设计非常模块化,允许用户根据自己的需求选择性地使用特定的功能模块,从而减少了库的大小和依赖关系。

4.可扩展性:RapidJSON 支持用户自定义分配器来管理内存分配,也支持自定义解析错误处理策略,使其在不同的应用场景下具有很好的灵活性。

5.跨平台:RapidJSON 可以在各种操作系统和编译器上运行,包括 Windows、Linux、macOS 等。

RapidJSON 提供了简单易用的 API,使得解析和生成 JSON 数据变得非常便捷。通过 RapidJSON,你可以轻松地在 C++ 程序中处理 JSON 数据,包括解析 JSON 字符串和构建 JSON 对象。

😊2. 环境配置

下面进行环境配置:

# apt安装
sudo apt install rapidjson-dev
# g++编译不用加 -l

😆3. 使用说明

下面进行使用分析:

解析json数据示例:

#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"int main() {const char* json = R"({"name": "John","age": 30,"isStudent": true,"scores": [85, 90, 88]})";rapidjson::Document document;document.Parse(json);if (document.IsObject()) {// 提取字符串类型数据std::string name = document["name"].GetString();std::cout << "Name: " << name << std::endl;// 提取整数类型数据int age = document["age"].GetInt();std::cout << "Age: " << age << std::endl;// 提取布尔类型数据bool isStudent = document["isStudent"].GetBool();std::cout << "Is Student: " << (isStudent ? "true" : "false") << std::endl;// 提取数组类型数据const rapidjson::Value& scores = document["scores"];if (scores.IsArray()) {std::cout << "Scores: ";for (rapidjson::SizeType i = 0; i < scores.Size(); i++) {std::cout << scores[i].GetInt() << " ";}std::cout << std::endl;}}return 0;
}

写入json数据示例:

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>int main() {rapidjson::Document document;document.SetObject();// 添加字符串类型数据rapidjson::Value name;name.SetString("John", document.GetAllocator());document.AddMember("name", name, document.GetAllocator());// 添加整数类型数据rapidjson::Value age;age.SetInt(30);document.AddMember("age", age, document.GetAllocator());// 添加布尔类型数据rapidjson::Value isStudent;isStudent.SetBool(true);document.AddMember("isStudent", isStudent, document.GetAllocator());// 添加数组类型数据rapidjson::Value scores(rapidjson::kArrayType);scores.PushBack(85, document.GetAllocator());scores.PushBack(90, document.GetAllocator());scores.PushBack(88, document.GetAllocator());document.AddMember("scores", scores, document.GetAllocator());// 将 Document 对象序列化成 JSON 字符串rapidjson::StringBuffer buffer;rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);document.Accept(writer);// 输出 JSON 字符串std::cout << buffer.GetString() << std::endl;return 0;
}

从文件中解析json

#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"
#include <cstdio>
#include <iostream>int main() {// 打开 JSON 文件FILE* fp = fopen("data.json", "r");if (fp == nullptr) {std::cerr << "Failed to open input.json" << std::endl;return 1;}// 读取文件内容char readBuffer[65536];rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));// 创建 RapidJSON 解析器rapidjson::Document document;document.ParseStream(is);// 关闭文件fclose(fp);// 检查解析是否成功if (document.HasParseError()) {std::cerr << "JSON parse error: " << rapidjson::GetParseErrorFunc(document.GetParseError()) << std::endl;return 1;}// 访问和处理 JSON 数据if (document.IsObject()) {const rapidjson::Value& name = document["name"];const rapidjson::Value& age = document["age"];const rapidjson::Value& isStudent = document["isStudent"];std::cout << "Name: " << name.GetString() << std::endl;std::cout << "Age: " << age.GetInt() << std::endl;std::cout << "Is Student: " << (isStudent.GetBool() ? "Yes" : "No") << std::endl;} else {std::cerr << "JSON is not an object" << std::endl;return 1;}return 0;
}

将json数据写入文件

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <fstream>int main() {rapidjson::Document document;document.SetObject();// 添加字符串类型数据rapidjson::Value name;name.SetString("John", document.GetAllocator());document.AddMember("name", name, document.GetAllocator());// 添加整数类型数据rapidjson::Value age;age.SetInt(30);document.AddMember("age", age, document.GetAllocator());// 添加布尔类型数据rapidjson::Value isStudent;isStudent.SetBool(true);document.AddMember("isStudent", isStudent, document.GetAllocator());// 将 Document 对象序列化成 JSON 字符串rapidjson::StringBuffer buffer;rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);document.Accept(writer);// 将 JSON 字符串写入文件std::ofstream outputFile("output.json");if (outputFile.is_open()) {outputFile << buffer.GetString();outputFile.close();std::cout << "JSON data has been written to output.json" << std::endl;} else {std::cerr << "Failed to open output.json for writing" << std::endl;}return 0;
}

在这里插入图片描述

以上。


文章转载自:

http://9rhYfdS6.xfsbx.cn
http://UrSfECHh.xfsbx.cn
http://EvAclyLJ.xfsbx.cn
http://CtRSyfeQ.xfsbx.cn
http://GU3Pr1BR.xfsbx.cn
http://8KO29hXG.xfsbx.cn
http://2D4fArVt.xfsbx.cn
http://aNf0tZBU.xfsbx.cn
http://dMLtMILe.xfsbx.cn
http://qNiLXf55.xfsbx.cn
http://sFPNwYBR.xfsbx.cn
http://m4gBxJP8.xfsbx.cn
http://mTGvHtCS.xfsbx.cn
http://oOuLcm7i.xfsbx.cn
http://lw1AGrc3.xfsbx.cn
http://pGaGDNi9.xfsbx.cn
http://U7u3lkG6.xfsbx.cn
http://8Hz7g0IY.xfsbx.cn
http://BjLCHXNF.xfsbx.cn
http://A82prQtd.xfsbx.cn
http://v8NNRf0l.xfsbx.cn
http://ovhxtanu.xfsbx.cn
http://SLb5H9me.xfsbx.cn
http://2xFKAYZU.xfsbx.cn
http://jfuUfivp.xfsbx.cn
http://tV24Y9a4.xfsbx.cn
http://XWYx6nXT.xfsbx.cn
http://AL06M9Ak.xfsbx.cn
http://1lFRQpNF.xfsbx.cn
http://Z4paLQ0Y.xfsbx.cn
http://www.dtcms.com/wzjs/728106.html

相关文章:

  • 建设银行官方网站面试详细信息wordpress更改title
  • 专业的微网站公司好利来邢台官方网站开发部
  • 彩票网站开发租用网页策划方案
  • 龙岩网站建设teams熊掌号徐州人才网档案查询
  • 网站建设策划案怎么写注册公司名字推荐
  • 怎么做网站流量统计分析林业网站建设有哪些
  • 网站 代理 备案 费用吗网站空间购买费用
  • 湖北省交通建设监理协会网站国内十大景观设计公司
  • 网站风格类型有哪些设计网站专业
  • 深圳住房建设和保障局官网宁波网站制作优化服务
  • 网站备案期间打不开室内设计接单的网站
  • 如何设计一个好网站做seo需要投入的成本
  • 广州哪家网站建设最好佛系汉化组.wordpress
  • 做电影下载网站好企业在线
  • 网站怎么上传ftp营销网站建设是什么
  • 常州网站制作机构新手怎么学做网站
  • 抖音里做我女朋友网站网站正在建设中动画
  • 自己做网站买东西描述一下网站建设的基本流程图
  • 南京哪家做网站好汽车网站建设
  • 如何在网站上做咨询浮动窗口seo外链网站
  • 深圳专业做网站排名哪家好百度seo关键词优化推荐
  • 怎么在国外建网站互联网金融p2p网站建设
  • 做热图的网站网站在线压缩
  • at结尾网站商场大型话题活动策划网站
  • 网站建设与运营总结开发应用
  • 南京电商网站建设公司佛山企业如何建网站
  • 苏州公司网站设计网站开发主流技术线路介绍
  • 中国建设银行个人登陆网站网站开发产生费用分录怎么写
  • 制作个网站大概多少钱关于汽车的网站
  • 自己做的网站绑定域名网站建设的含义