c++实现对rapidjson的封装,更方便的用户接口

本文提供了一个C++类Rjson,用于封装rapidjson库,简化JSON对象的访问和操作。通过GetObject、GetArray、GetStr和GetInt等静态方法,可以方便地获取JSON对象的值,支持多层路径定位。
摘要由CSDN通过智能技术生成
/*******************************************************************
* summery: 提供便捷的方法操作rapidjson
* author: hejl
* date: 2017-02-17
* description: 有效避免string(NULL), element=NULL, strdup(NULL)判断
******************************************************************/

#ifndef _JSON_HPP_
#define _JSON_HPP_
#include <string>
#include "document.h"
#include "rapidjson.h"
#include "prettywriter.h"

using namespace rapidjson;
using std::string;


/*****************************
关键字说明:
Object : json中的{key: value}形式类型
Array : json中的[e1, e2, e3]形式类型
Value : 对json数据类型的抽象,包括Object,Array,int,string,是Object和Array的基类
GetStr/GetInt : 获取json内部某一部分的字符串值,通通1st参数返回,最后一参数是父节点Value,
中间参数用于定位第几元素(参数类型是int)或指定key对应的值(参数类型是const char*)

usage example:
Document doc;
char jsonstr[] = "{ \"key1\": 123, \"key2\": \"string value\", \"key3\": [100, 200, \"str300\"] }"
doc.Parse<0>(jsonstr.c_str());
if (doc.HasParseError()) { ... }
// get key2 string value
std::string val2;
int result = Rjson::GetValue(val2, "key2", &doc);
// get key1 integer value
int val1;
int result = Rjson::GetValue(val1, "key1", &doc);
// get array [1] integer of key3
int val3_1;
int result = Rjson::GetValue(val3_1, "key3", 1, &doc);
// get array [2] string of key3
string val3_2;
int result = Rjson::GetValue(val3_2, "key3", 2, &doc);
*****************************/

class Rjson
{
public:
static int GetValue( const Value ** value,
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值