1. RapidJSON 介绍

RapidJSON 是一个解析和生成 JSON 的 C++ 库。
无需编译,包含头文件即可使用。

2. RapidJSON 使用

2.1 解析

    const char* json = "{""\"stringValue\": \"hello world\",""\"boolTrue\": true,""\"boolFalse\": false,""\"nullValue\": null,""\"intValue\": 3498,""\"floatValue\": 3.14,""\"arrayValue\": [1, 2, 3, 4]""}";// parse jsonDocument document;document.Parse(json);assert(document.IsObject());

2.2 查询

2.2.1 查询字符串

    // query stringconst char* STRING_VALUE = "stringValue";assert(document.HasMember(STRING_VALUE));assert(document[STRING_VALUE].IsString());std::cout << STRING_VALUE << " = " << document[STRING_VALUE].GetString() << std::endl;

2.2.2 查询 Bool 值

    // query boolconst char* BOOL_TRUE = "boolTrue";assert(document.HasMember(BOOL_TRUE));assert(document[BOOL_TRUE].IsBool());assert(document[BOOL_TRUE].IsTrue());std::cout << BOOL_TRUE << " = " << std::boolalpha << document[BOOL_TRUE].GetBool() << std::endl;const char* BOOL_FALSE = "boolFalse";assert(document.HasMember(BOOL_FALSE));assert(document[BOOL_FALSE].IsBool());assert(document[BOOL_FALSE].IsFalse());std::cout << BOOL_FALSE << " = " << std::boolalpha << document[BOOL_FALSE].GetBool() << std::endl;

2.2.3 判断是否为 Null 值

    // query null valueconst char* NULL_VALUE = "nullValue";assert(document.HasMember(NULL_VALUE));std::cout << NULL_VALUE << " = " << (document[NULL_VALUE].IsNull() ? "null" : "?") << std::endl;

2.2.4 查询 Int 值

    // query int valueconst char* INT_VALUE = "intValue";assert(document.HasMember(INT_VALUE));assert(document[INT_VALUE].IsNumber());assert(document[INT_VALUE].IsInt());assert(document[INT_VALUE].IsInt64());assert(document[INT_VALUE].IsUint());assert(document[INT_VALUE].IsUint64());std::cout << INT_VALUE << "(int) = " << document[INT_VALUE].GetInt() << std::endl;std::cout << INT_VALUE << "(uint) = " << document[INT_VALUE].GetUint() << std::endl;std::cout << INT_VALUE << "(int64) = " << document[INT_VALUE].GetInt64() << std::endl;std::cout << INT_VALUE << "(uint64) = " << document[INT_VALUE].GetUint64() << std::endl;

2.2.5 查询 Float 值

    // query float valueconst char* FLOAT_VALUE = "floatValue";assert(document.HasMember(FLOAT_VALUE));assert(document[FLOAT_VALUE].IsNumber());assert(document[FLOAT_VALUE].IsFloat());assert(document[FLOAT_VALUE].IsDouble());std::cout << FLOAT_VALUE << "(float) = " << document[FLOAT_VALUE].GetFloat() << std::endl;std::cout << FLOAT_VALUE << "(double) = " << document[FLOAT_VALUE].GetDouble() << std::endl;

2.2.6 查询数组

    // query arrayconst char* ARRAY_VALUE = "arrayValue";const Value& arrayValue = document[ARRAY_VALUE];assert(arrayValue.IsArray());for (SizeType i = 0; i != arrayValue.Size(); ++i){assert(arrayValue[i].IsInt());std::cout << ARRAY_VALUE << "[" << i << "] = " << arrayValue[i].GetInt() << std::endl;}for (Value::ConstValueIterator iter = arrayValue.Begin(); iter != arrayValue.End(); ++iter){assert(iter->IsInt());std::cout << iter->GetInt() << std::endl;}// Range-based For Loop, C++11 onlyfor (auto& value : arrayValue.GetArray()){assert(value.IsInt());std::cout << value.GetInt() << std::endl;}

2.2.7 查询对象

    // query objectstatic const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };for (Value::ConstMemberIterator iter = document.MemberBegin(); iter != document.MemberEnd(); ++iter){std::cout << "Type of member " << iter->name.GetString() << " is " << kTypeNames[iter->value.GetType()] << std::endl;}// Range-based For Loop, C++11 onlyfor (auto& member : document.GetObject()){std::cout << "Type of member " << member.name.GetString() << " is " << kTypeNames[member.value.GetType()] << std::endl;}// find memberValue::ConstMemberIterator iter = document.FindMember(STRING_VALUE);if (iter != document.MemberEnd()){std::cout << STRING_VALUE << " = " << iter->value.GetString() << std::endl;}

3.完整的例子

#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include <iostream>
#include <string>void testQuery()
{using namespace rapidjson;const char* json = "{""\"stringValue\": \"hello world\",""\"boolTrue\": true,""\"boolFalse\": false,""\"nullValue\": null,""\"intValue\": 3498,""\"floatValue\": 3.14,""\"arrayValue\": [1, 2, 3, 4]""}";// parse jsonDocument document;document.Parse(json);assert(document.IsObject());// query stringconst char* STRING_VALUE = "stringValue";assert(document.HasMember(STRING_VALUE));assert(document[STRING_VALUE].IsString());std::cout << STRING_VALUE << " = " << document[STRING_VALUE].GetString() << std::endl;// query boolconst char* BOOL_TRUE = "boolTrue";assert(document.HasMember(BOOL_TRUE));assert(document[BOOL_TRUE].IsBool());assert(document[BOOL_TRUE].IsTrue());std::cout << BOOL_TRUE << " = " << std::boolalpha << document[BOOL_TRUE].GetBool() << std::endl;const char* BOOL_FALSE = "boolFalse";assert(document.HasMember(BOOL_FALSE));assert(document[BOOL_FALSE].IsBool());assert(document[BOOL_FALSE].IsFalse());std::cout << BOOL_FALSE << " = " << std::boolalpha << document[BOOL_FALSE].GetBool() << std::endl;// query null valueconst char* NULL_VALUE = "nullValue";assert(document.HasMember(NULL_VALUE));std::cout << NULL_VALUE << " = " << (document[NULL_VALUE].IsNull() ? "null" : "?") << std::endl;// query int valueconst char* INT_VALUE = "intValue";assert(document.HasMember(INT_VALUE));assert(document[INT_VALUE].IsNumber());assert(document[INT_VALUE].IsInt());assert(document[INT_VALUE].IsInt64());assert(document[INT_VALUE].IsUint());assert(document[INT_VALUE].IsUint64());std::cout << INT_VALUE << "(int) = " << document[INT_VALUE].GetInt() << std::endl;std::cout << INT_VALUE << "(uint) = " << document[INT_VALUE].GetUint() << std::endl;std::cout << INT_VALUE << "(int64) = " << document[INT_VALUE].GetInt64() << std::endl;std::cout << INT_VALUE << "(uint64) = " << document[INT_VALUE].GetUint64() << std::endl;// query float valueconst char* FLOAT_VALUE = "floatValue";assert(document.HasMember(FLOAT_VALUE));assert(document[FLOAT_VALUE].IsNumber());assert(document[FLOAT_VALUE].IsFloat());assert(document[FLOAT_VALUE].IsDouble());std::cout << FLOAT_VALUE << "(float) = " << document[FLOAT_VALUE].GetFloat() << std::endl;std::cout << FLOAT_VALUE << "(double) = " << document[FLOAT_VALUE].GetDouble() << std::endl;// query arrayconst char* ARRAY_VALUE = "arrayValue";const Value& arrayValue = document[ARRAY_VALUE];assert(arrayValue.IsArray());for (SizeType i = 0; i != arrayValue.Size(); ++i){assert(arrayValue[i].IsInt());std::cout << ARRAY_VALUE << "[" << i << "] = " << arrayValue[i].GetInt() << std::endl;}for (Value::ConstValueIterator iter = arrayValue.Begin(); iter != arrayValue.End(); ++iter){assert(iter->IsInt());std::cout << iter->GetInt() << std::endl;}// Range-based For Loop, C++11 onlyfor (auto& value : arrayValue.GetArray()){assert(value.IsInt());std::cout << value.GetInt() << std::endl;}// query objectstatic const char* kTypeNames[] = { "Null", "False", "True", "Object", "Array", "String", "Number" };for (Value::ConstMemberIterator iter = document.MemberBegin(); iter != document.MemberEnd(); ++iter){std::cout << "Type of member " << iter->name.GetString() << " is " << kTypeNames[iter->value.GetType()] << std::endl;}// Range-based For Loop, C++11 onlyfor (auto& member : document.GetObject()){std::cout << "Type of member " << member.name.GetString() << " is " << kTypeNames[member.value.GetType()] << std::endl;}// find memberValue::ConstMemberIterator iter = document.FindMember(STRING_VALUE);if (iter != document.MemberEnd()){std::cout << STRING_VALUE << " = " << iter->value.GetString() << std::endl;}
}void test1()
{// 解析jsonconst char* json = "{\"project\":\"rapidjson\",\"stars\":10}";rapidjson::Document d;d.Parse(json);// 获取值rapidjson::Value& s = d["stars"];  std::cout << "stars: " << s.GetInt() << std::endl;// 设置值s.SetInt(s.GetInt() + 1);// 获取jsonrapidjson::StringBuffer buffer;rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);d.Accept(writer);std::cout << buffer.GetString() << std::endl;
}void test2()
{// 创建jsonrapidjson::Document document;rapidjson::Document::AllocatorType& allocator = document.GetAllocator();rapidjson::Value root(rapidjson::kObjectType);root.AddMember("project", "rapidjson", allocator);root.AddMember("stars", 12, allocator);rapidjson::StringBuffer buffer;rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);root.Accept(writer);std::string reststring = buffer.GetString();std::cout << reststring << std::endl;
}int main(void)
{testQuery();test1();test2();   return 0;
}

RapidJSON 介绍相关推荐

  1. rapidjson安装学习

    这里主要记录几个要点,后面来补充吧,很晚了 源码是鹅厂大佬写的,佩服佩服~ 一.RapidJSON介绍及资料 RapidJSON是腾讯开源的C++ JSON解析及生成器,只有头文件的C++库,跨平台. ...

  2. RapidJSON入门:手把手教入门实例介绍

    RapidJSON优点 跨平台 编译器:Visual Studio.gcc.clang 等 架构:x86.x64.ARM 等 操作系统:Windows.Mac OS X.Linux.iOS.Andro ...

  3. RapidJSON简介及使用

    RapidJSON是腾讯开源的一个高效的C++ JSON解析器及生成器,它是只有头文件的C++库.RapidJSON是跨平台的,支持Windows, Linux, Mac OS X及iOS, Andr ...

  4. 推荐一款cpp解析json工具--rapidjson

    项目地址:http://code.google.com/p/rapidjson/ 上面有很详细的介绍:http://code.google.com/p/rapidjson/wiki/UserGuide ...

  5. rapidjson官方教程

    原文地址:http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html 教程 目录 Value 及 Document 查询Value 查询Array 查 ...

  6. 三、安装cmake,安装resin ,tars服务,mysql 安装介绍,安装jdk,安装maven,c++ 开发环境安装...

    三.安装cmake,安装resin 2018年07月01日 21:32:05 youz1976 阅读数:308 开发环境说明: centos7.2 ,最低配置:1核cpu,2G内存,1M带宽 1.安装 ...

  7. JSON--rapidjson介绍

    JSON--rapidjson 1 RapidJSON简介 2 C/C++ Json库对比 一致性 解析时间 解析内存 Stringify Time(string 2 json) Prettify T ...

  8. RapidJSON 代码剖析(三):Unicode 的编码与解码

    根据 RFC-7159: 8.1 Character Encoding JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32. The defa ...

  9. RapidJSON v1.1.0 发布简介

    时隔 15.6 个月,终于发布了一个新版本 v1.1.0. 新版本除了包含了这些日子收集到的无数的小改进及 bug fixes,也有一些新功能.本文尝试从使用者的角度,简单介绍一下这些功能和沿由. P ...

最新文章

  1. 凸透镜成像实验软件_中考物理凸透镜成像难点解析
  2. spring之旅第四篇-注解配置详解
  3. lua 区间比较_Lua(模糊查找):判断两个字符串(含中文)是否存在至少一个相同
  4. 如何在数字化转型战略中真正获得价值?浅谈数字化转型的四个层级
  5. unix系统重启tcp服务器,《TCP/IP详解卷3:TCP事务协议、HTTP、NNTP和UNIX域协议》 —3.5 服务器重启动...
  6. 极客大学架构师训练营--食堂就餐系统架构设计⽂档 -- 第一次作业
  7. gbk utf-8 asccl url
  8. 社会工程学——基础认知(补充)
  9. 萌言萌语|测试工作日报及总结
  10. 水瓶座06年3月运程
  11. Nautre综述:鸟枪法宏基因组-从取样到数据分析(1)2万字带你系统入门宏基因组实验和分析...
  12. matlab之经验分布图
  13. 基于飞凌FETA40i-C核心板在光时域反射仪中的应用原理
  14. Hypervisor操作系统间的通信技术
  15. 二分查找算法(非递归)
  16. 自动化当道,破密、爬虫各凭本事(GitHub 热点速览 Vol.37)
  17. android 测试机 怎么root,Android 应用安全 - 检测设备是否Root
  18. 银河麒麟系统安装部署软件
  19. 计算机专业学生毕业去大公司好还是小公司好?
  20. 2021/12/15

热门文章

  1. ButterKnife Zelezny使用
  2. 20210421用一条电线和一颗电池点亮灯泡,麻省理工(MIT)毕业生竟然不会?
  3. 洛谷P2168 荷马史诗 [NOI2015]
  4. 随机看妹子_这是不可能的
  5. 修改apn联通服务器地址,联通服务器apn地址
  6. linux-ubuntu终端切换桌面方法
  7. spring boot之maven-wrapper
  8. 微信小程序引入阿里巴巴图标库
  9. 进程间通信——几种方式的比较和详细实例
  10. 12款最佳的Linux命令行终端工具