首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用rapidjson更新数组?

如何使用rapidjson更新数组?
EN

Stack Overflow用户
提问于 2016-01-12 16:09:06
回答 1查看 3.4K关注 0票数 0

我用rapidjson做了一些事情,我想把值添加到我刚才创建的数组中

代码语言:javascript
复制
#include <iostream>
#include "rapidjson/document.h"
using namespace std ;

int main() {


    char json[1024];
    rapidjson::Document document ;
    document.Parse<0>(json);
    if (!document.IsObject()) {
            document.SetObject();
    }
    assert(document.IsObject());
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
    // adding member (int)
    document.AddMember("mohammed",25,allocator);
    assert(document.HasMember("mohammed"));
    cout << document["mohammed"].GetInt() << endl ;

    // adding member (array)
    rapidjson::Value array(rapidjson::kArrayType);
    array.PushBack(5,allocator);
    array.PushBack(6,allocator);
    cout << array[0u].GetInt() << endl ;
    cout << array[1].GetInt() << endl ;
    document.AddMember("array",array,allocator);
    assert(document.HasMember("array"));
    assert(document["array"].IsArray());
    // here the following line give me an error 
    array.PushBack(7,allocator);




}

错误是

代码语言:javascript
复制
json: rapidjson/document.h:397: rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::PushBack(rapidjson::GenericValue<Encoding, Allocator>&, Allocator&) [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `IsArray()' failed.

中止(核心倾弃)

有人能解释什么问题?我对这件事有点陌生,谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-18 01:58:02

在执行array.PushBack(...)时,array已经被移动document中,并成为null值类型(array.IsNull() == true)。因此,您不能将PushBack设置为空值。

document["array"].PushBack(7,allocator)会工作的。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34748465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档