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

在c++项目中使用rapidjson(有具体的步骤,十分详细) windows10系统

具体的步骤:

  • 先下载rapidjson的依赖包

方式1:直接使用git去下载

地址:git clone https://github.com/miloyip/rapidjson.git

方式2:下载我上传的依赖包

  • 将依赖包引入到项目中

1  将解压后的文件放在你c++项目中

2 将rapidjson文件中的src目录引入到项目中

步骤:

1        打开项目,项目属性

 2 点击属性 -----》c++------》添加src目录--》确认退出

到这里你的项目就可以用rapidjson了

  • 开始使用rapidjson(有一些需要注意的地方)

创建头文件

#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 <iostream>
#include <fstream>
#include <stdio.h>


using namespace rapidjson;
using namespace std;


int rapidJsonTest();

创建cpp文件

#include "rapidJsonTest.h"
#include <vector>
#include "memory.h"
#include<ctime>
#include<stdio.h>

int rapidJsonTest()
{

 Document jsonDoc;
    Document::AllocatorType& allocator = jsonDoc.GetAllocator();
    jsonDoc.SetObject();

    //主对象
    Value value(kObjectType);
    value.AddMember("flag", 0, allocator);
    value.AddMember("msg", "操作成功", allocator);


    //result对象
    Value result(kObjectType);
    //添加result的基本信息
    result.AddMember("name", "(匿名)", allocator);
    result.AddMember("right_query", true, allocator);

    //创建一个array对象,用于存储t信息
    Value arraych(kArrayType);
    struct  chunnels
    {
        int chunnel;
        string username;
        string telphone;
    };

    
    for (int i = 0; i < 5; i++) {
        chunnels ch;
        ch.chunnel = 0;
        ch.username = "黎明";
        ch.telphone = "110";

        Value chu(kObjectType);
        Value v0;
        v0.SetInt(ch.chunnel);                //数值型这样写


        Value v1(ch.username.c_str(), allocator);   //字符串这样写就不会出现/u0000的问题了
        Value v2(ch.telphone.c_str(), allocator);

        chu.AddMember("chunnel", v0, allocator);
        chu.AddMember("username", v1, allocator);
        chu.AddMember("telphone", v2, allocator);

        chu.AddMember("第三种写法", "6666", allocator);   //直接内容的话,不需要转

        arraych.PushBack(chu, allocator);
    }
    
 

    //将集合添加到result对象中
    result.AddMember("chunnels", arraych, allocator);
    //将result对象加入到主对象
    value.AddMember("result", result, allocator);

    // 转为string
    StringBuffer str;
    Writer<StringBuffer> writer(str);
    value.Accept(writer);
    string strJson = str.GetString();
    cout << strJson << endl;

    return 0;
}

案例二

   int demo2(){

   cout << "json Value的新建及key的访问" << endl;

    Document jsonDoc;    //生成一个dom元素Document
    Document::AllocatorType& allocator = jsonDoc.GetAllocator(); //获取分配器
    jsonDoc.SetObject();    //将当前的Document设置为一个object,也就是说,整个Document是一个Object类型的dom元素

    // 新建Value对象1(object类型)
    Value value1(kObjectType);
    value1.AddMember("name", "语文", allocator);             // string型(给字段赋值,key必须为string型下同)
    value1.AddMember("score", 80, allocator);             // 整型
    value1.AddMember("right", true, allocator);           // 整型
    value1.AddMember("percent", 12.3456789123, allocator);// double型

    // 此时访问key是可以的
    if (value1.HasMember("name")) // 判断是否存在该key
    {
        if (value1["name"].IsString()) // 再判断类型是否正确
        {
            cout << "value1:name:" << value1["name"].GetString() << endl;
        }
        cout << "value1:score:" << value1["score"].GetInt() << endl; // 直接这样写有风险
    }

    // 新建Value对象(数组类型)
    Value value2(kArrayType);
    value2.PushBack(1, allocator);
    value2.PushBack(2, allocator);
    value2.PushBack(3, allocator);
    cout << "value:size()数组中元素个数:" << value2.Size() << endl;

    // 合并一个整体
    Value value3(kObjectType);
    value3.AddMember("name", "xiaoming", allocator);
    value3.AddMember("age", 18, allocator);
    value3.AddMember("value1", value1, allocator);        // 整个value1作为key的值
    value3.AddMember("value2", value2, allocator);        // 整个value2作为key的值

    // 转为string
    StringBuffer str;
    Writer<StringBuffer> writer(str);
    value3.Accept(writer);
    string strJson = str.GetString();
    cout << "value3:" << strJson.c_str() << endl;

}

记得在使用rapidjson是,对于string类型和数值类型,需要使用rapidjson的形式去封装,不然会出错

http://www.lryc.cn/news/65192.html

相关文章:

  • 编译方式汇总:Makefile\configure\autogen.sh\configure.ac、Makefile.am文件
  • explicit关键字
  • [优雅的面试] 你了解python的对象吗
  • 【hello Linux】线程概念
  • JavaWeb07(MVC应用01[家居商城]连接数据库)
  • 如何使用电商API接口API接口如何应用
  • 【移动端网页布局】流式布局案例 ⑥ ( 多排按钮导航栏 | 设置浮动及宽度 | 设置图片样式 | 设置文本 )
  • 1. 先从云计算讲起
  • ZooKeeper安装与配置集群
  • 浅谈Mysql的RR和RC隔离级别的主要区别
  • Build生成器模式
  • C++程序设计——常见C++11新特性
  • Rust main 函数返回值类型不能是 String
  • 视频里的音乐怎么转换成mp3格式?
  • CSS3 grid网格布局
  • SPSS如何进行均值比较和T检验之案例实训?
  • Packet Tracer - 配置交换机端口安全
  • 一图看懂 aiohttp 模块:基于 asyncio 的异步HTTP网络库, 资料整理+笔记(大全)
  • Linux + 香橙派 + V4L2 + http 实现远程监控摄像头在网页端显示
  • 《编码——隐匿在计算机软硬件背后的语言》精炼——第15-16章(十六进制,RAM)
  • leetcode.1376 通知所有员工所需的时间 - bfs/dfs + 树
  • AtCoder Beginner Contest 300——A-G题讲解
  • Go:值与指针
  • 【Linux】进程学习(2)---理解进程操作
  • 基于springcloud实现的医院信息系统
  • 设计模式-创建型模式-(工厂、简单工厂、抽象工厂)
  • JAVA12新特性
  • Nginx 静态文件、反向代理、负载均衡、缓存、SSL/TLS 加密、gzip 压缩 等等
  • Linux设备驱动模型(一)
  • 【Python入门篇】——Python基础语法(标识符与运算符)