rapidjson的下载以及简单使用

一、rapidjson的下载

下载地址:https://github.com/Tencent/rapidjson

二、rapidjson的使用

1.windows平台下的使用

(1) 将所下载文件中的include目录复制到项目文件夹
(2) 点击项目 --> 属性 -->配置属性–>包含目录把include所对应的路径添加进去
在这里插入图片描述

2.linux下的使用

(1)也是将include文件夹复制到对应的项目文件夹里
(2)使用g++ -I /linux上include的路径 main.cpp -o main 就会生成main的可执行文件

二、rapidjson 的简单使用及读写txt文件

1.给json插入字段

#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/istreamwrapper.h"
#include "rapidjson/ostreamwrapper.h"
#include <fstream>

using namespace std;
using namespace rapidjson;

Document jsonDoc;    //生成一个dom元素Document
Document::AllocatorType &allocator = jsonDoc.GetAllocator(); //获取分配器
jsonDoc.SetObject();
Value value1(kObjectType);
value1.AddMember("name", "语文", allocator);		     // string型(给字段赋值,key必须为string型下同)
value1.AddMember("score", 80, allocator);             // 整型
value1.AddMember("right", true, allocator);           // bool
value1.AddMember("percent", 12.3456789123, allocator);//浮点型

2.将json写入txt

void writeFiles(string str,string FileName)			//写入json文件
{
    Document docu;
    docu.Parse(str.c_str());//将字符串载入json
    ofstream ofs(FileName.c_str());
    OStreamWrapper osw(ofs);
    Writer<OStreamWrapper> writer3(osw);
    docu.Accept(writer3);
}

3.读取txt

void  ReadJson(Document d,string FileName)
{
    FILE* fp = fopen(FileName.c_str(), "r+");
    char readBuffer[65536];
    FileReadStream is(fp, readBuffer,sizeof(readBuffer));
    d.ParseStream(is);
    fclose(fp);
    return d;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用说明: 1.易语言模块和使用例子在Release目录下 2.为了节省打包大小,删除了Visual Studio的配置,重新编译的话 选择Release x86即可 3.官方库若有更新可直接替换include文件夹,重新编译即可(官方git: https://github.com/Tencent/rapidjson/)(官方文档: http://rapidjson.org/zh-cn/) 封装日志: 1.0.0.9版-2019.5.6 1) 升级 rapidjson库到官方最新版本(2019.4.15) 1.0.0.8版-2018.11.22 1)  修复 gstrlen函数 pop顺序错误问题. 2)  修复 win10环境下【SAX解析】路径深度到达3时,路径未以0结尾问题. 1.0.0.7版-2018.11.17 1)  修复 NumConversion.h中 StrToInt64函数 转换异常问题。(所有取长整数值,若类型是文本型,自动转换时会调用该函数) 2)  升级 rapidjson库到官方最新版本(2018.10.8) 1.0.0.6版-2018.10.8 1)  修复 rapidjson_dll_ec.e RJ生成W.创建对象和RJ生成W.创建数组 键名为空时,生成异常问题 2)  优化 取数值时,若为文本型,则强转为对应数值返回. 3)  添加 通配_取xx值配置 系列 (作用:取值,需要提供一个默认值,若节点存在则返回节点值,不存在则添加默认值) 4)  添加 通配_置xx值 系列 (作用:可多路径生成json) 5)  添加 pointer_erase_path 函数 (作用:删除某个节点) 6)  添加 pointer_is_exist 函数 (作用: 查询节点是否存在) 7)  添加 几个性能优化过的辅助函数,实现在rapidjson_dll_ec.e(辅助功能) 8)  封装 zlib部分解压缩功能,实现在auxiliary.cpp 9)  更新 易语言模块和使用例子 1.0.0.5版-2018.9.26 1)  添加SAX解析方式,实现在sax.cpp 2)  同步更新使用例子(rapidjson.e) 1.0.0.4版-2018.9.9 1)  修复解析时传入空指针导致奔溃问题 2)  修复一些隐患 3)  增加object_get_key函数(取对象成员键名) 4)  增加double_to_string函数(双精度到文本 Grisu2算法),实现在auxiliary.cpp 5)  同步更新易语言模块和使用例子 1.0.0.3版-2018.8.30 1)  修复object_get_int和get_path_type返回错误问题(测试的时候加了个取字符串长度的代码,忘记删掉了- -)
使用 RapidJSON 库将一个 JSON 对象添加到另一个 JSON 对象中,可以使用 `AddMember()` 函数。以下是一个示例: ```cpp #include <iostream> #include <rapidjson/document.h> #include <rapidjson/writer.h> #include <rapidjson/stringbuffer.h> using namespace rapidjson; int main() { // 创建一个 JSON 对象 Document document; document.SetObject(); // 创建要添加的 JSON 对象 Value innerObject(kObjectType); Value key("name"); Value value("John"); innerObject.AddMember(key, value, document.GetAllocator()); // 将内部对象添加到外部对象 Value keyOuter("person"); document.AddMember(keyOuter, innerObject, document.GetAllocator()); // 将 JSON 对象转换为字符串 StringBuffer buffer; Writer<StringBuffer> writer(buffer); document.Accept(writer); std::string jsonString = buffer.GetString(); // 打印 JSON 字符串 std::cout << jsonString << std::endl; return 0; } ``` 在上述示例中,我们首先创建了一个外部的 JSON 对象 `document`。然后,我们创建了一个要添加到外部对象中的内部对象 `innerObject`。接下来,我们使用 `AddMember()` 函数将内部对象添加到外部对象中,并指定键名为 `"person"`。最后,我们将整个 JSON 对象转换为字符串形式。 输出结果示例: ```json {"person":{"name":"John"}} ``` 这是一个简单的示例,展示了如何使用 RapidJSON 库将一个 JSON 对象添加到另一个 JSON 对象中。你可以根据需要修改和扩展这个示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BigProgrambug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值