前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ RapidJson常用用法示例

C++ RapidJson常用用法示例

作者头像
Cloudox
发布2021-11-23 16:46:15
2.3K0
发布2021-11-23 16:46:15
举报
文章被收录于专栏:月亮与二进制月亮与二进制

C++对Json字符串和对象的转换使用没有直接的库,所以RapidJson就成了最常用的解析库,教程有大量详尽的用法示例,这里仅筛选出最常用的用法做完整示例,包括:

  • 字符串转json对象
  • json对象键值对获取
  • 整数值对象的添加&查询
  • 浮点数值对象的添加&查询
  • 字符串对象的添加&查询
  • 数组对象的添加&查询
  • 子对象的添加&查询
  • json对象转回字符串

假设我们逐渐构造这样一个json:

代码语言:javascript
复制
{
"name":"Cloudox",
"age":18,
"gender":"male",
"height":188.8,
"hobby":["read","code","movie","game","walk"],
"university":{
    "name":"HUST",
    "location":"wuhan"
    }
}

代码如下:

代码语言:javascript
复制
#include <iostream>
#include "rapidjson/document.h"
#include <vector>
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

using namespace std;
using namespace rapidjson;

int main() {
    // json字符串
    string jsonStr = "{\"name\": \"Cloudox\", \"age\": 18}";

    // 转成json对象
    char* json = (char*)jsonStr.c_str();
    Document document;
    document.Parse(json);
    cout << "Hello World, I'm " << document["name"].GetString() << ", age " << document["age"].GetInt() <<  endl;
    
    // 添加字符串值的节点
    Value str;
    str = "male";
    document.AddMember("gender", str, document.GetAllocator());// 参数:key、value

    // 添加double节点
    Value doub;
    doub =  188.8;
    document.AddMember("height", doub, document.GetAllocator());

    cout << "my gender is " << document["gender"].GetString() << " and height is " << document["height"].GetDouble() <<  endl;

    // 添加数组
    Value arr(kArrayType);
    Document::AllocatorType& allocator = document.GetAllocator();
    vector<string> vec = {"read", "code", "movie", "game", "walk"};
    for (int i = 0; i < 5; i++) {
        // 这里很奇怪的是直接放vec[i]编译不通过,不得不转char*再转StringRef
        arr.PushBack(StringRef(vec[i].c_str()), allocator);   // 可能需要调用 realloc() 所以需要 allocator
    }
    // arr.PushBack("read", allocator).PushBack("code", allocator); // 另一种组装数组的方式
    document.AddMember("hobby", arr, allocator);
    
    cout << "my hobby:" << endl;
    // 使用引用来连续访问,方便之余还更高效。
    const Value& a = document["hobby"];
    assert(a.IsArray());// 所有类型的值都可以先进行判断
    for (SizeType i = 0; i < a.Size(); i++) // PS: Array 与 std::vector 相似,除了使用索引,也可使用迭代器来访问所有元素。
        cout << a[i].GetString() << endl;

    // 添加一个json子对象
    Value uni(kObjectType);
    uni.AddMember("name", "HUST", allocator);
    uni.AddMember("location", "wuhan", allocator);
    document.AddMember("university", uni, allocator);

    cout << "My university is " << document["university"]["name"].GetString() << " which locate in " << document["university"]["location"].GetString() << endl;

    // 转成字符串
    StringBuffer strBuffer;  
    Writer<StringBuffer> writer(strBuffer);  
    document.Accept(writer);  
    cout << strBuffer.GetString() << endl;
    
    system("pause");// 暂停以显示终端窗口
    return 0;
}

注释很详尽,就不细说了,工程在此:https://github.com/Cloudox/RapidJsonCppDemo

参考: 官方文档:http://rapidjson.org/zh-cn/md_doc_tutorial_8zh-cn.html rapidjson库的基本使用:https://blog.csdn.net/qq849635649/article/details/52678822


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/5/5 下午,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档