rapidjson查询操作基本用法

博客搬家,原地址:https://langzi989.github.io/2017/05/27/rapidjson查询操作基本用法/

本系列文章以例子的方式进行呈现。

#include "rapidjson/document.h"
#include <iostream>

int main()
{
   
  const char* test = "{\"num\":123, \"hello\":null, \"type\": \"object\",\"properties\": {\"oid\": {\"type\": \"string\"}, \"username\": {\"type\": \"string\"},\"creid\": {\"type\": \"string\"}},\"required\": [\"oid\", \"username\",\"creid\"]}";
  rapidjson::Document document;
  //Parse(const char*). 从c-string解析为Document json格式
  document.Parse(test);

  //GetString().从Document json中取出string
  std::cout << "type = " << document["type"].GetString() << std::endl;

  /*  函数原型          函数功能
  *   IsNull()       判断当前键对应的值是不是null
  *   IsNumber()     判断当前键对应的值是不是number
  *   IsInt()        判断当前键对应的值是不是int
  *   IsDouble()     判断当前键对应的值是不是double
  *   IsString()     判断当前键对应的值是不是string
  *   IsBool()       判断当前键对应的值是不是bool
  *   IsArray()      判断当前键对应的值是不是array
  *   ...
  */
  std::cout << "hello is " << (document["hello"].IsNull() ? "null" : "not null") << std::endl;
  std::cout << "num is " << (document["num"].IsNumber() ? "number" : "not number") << std::endl;
  std::cout << "required is " << (document["required"].IsArray()? "array" : "not array") << std::endl;

  /*      访问Array的两种方法
  *    1. 通过下标访问    //使用引用来连续访问,方便之余还更高效。
  *    2. 通过迭代器访问
  *        注意问题
  *    1.索引使用SizeType类型,而不是size_t类型,缺省情况下,SizeType是unsigned的typedef
  *    2.对于String类型使用GetInt是非法的,这个时候会导致程序崩溃
  */
  const rapidjson::Value &a = document["required"];
  assert(a.IsArray());
  for (rapidjson::SizeType i = 0; i < a.Size(); i++)
    std
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值