RapidJson简单使用(1)

1. RapidJson简介

RapidJson是一个C++的JSON解析器和生成器。RapidJson具有小而全,高性能,跨平台、不依赖外部库而独立,对Unicode友好和内存友好等特点。 

2.RapidJson安装

RapidJson依赖cmake作为通用生成工具,以Ubuntu为例,安装步骤如下:
    1.下载RapidJson库:
        #git submodule update --init        
    2.在RapidJson库目录下,创建一个build目录:
        #mkdir build
    3.在build目录下执行:
        #cmake ..
    4.在 Linux/Ubuntu 下,于 build 目录运行: 
        #make 
    注意:在 Windows 下,编译生成在 build 目录中的 solution。
    5.在buil目录下执行:
        #make install

2.RapidJson简单使用


#include <iostream>
#include <string>
#include <assert.h>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

using namespace std;
using namespace rapidjson;

int main(void)
{
    //1.把Json解析至DOM
    //const char *json ="{\"project\":\"rapidjson\",\"stars\":10}";
    const char *json = "{\"project\":\"rapidjson\",\"hello\":\"welcome to the rapidjson world\",\"year\":2018,\"month\":6,\"day\":18}";
    Document d;
    d.Parse(json);

    //2.通过GetInt()或SetInt()等方法获取或修改Json对用的数据
    string str = d["project"].GetString();
    str.append(",");
    str +=d["hello"].GetString();
    int year,month,day;
    year = d["year"].GetInt();
    month = d["month"].GetInt();
    day = d["day"].GetInt();
    cout<<str.c_str()<<endl;
    cout<<"Date:"<<year<<"-"<<month<<"-"<<day<<endl;

    //3.把DOM转换成JSON
    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    cout<<buffer.GetString()<<endl;
    return 0;
}
最后,通过编译运行,结果如下:

这里写图片描述

初次使用rapidjson,有很多东西还需要时间去消化,花时间去研究源码,深入学习,多练习,多总结,才能很好的使用rapidjson库。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值