在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的形式去封装,不然会出错

猜你喜欢

转载自blog.csdn.net/weixin_45533131/article/details/130468147