我用rapidjson做了一些事情,我想把值添加到我刚才创建的数组中
#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);
}错误是
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.中止(核心倾弃)
有人能解释什么问题?我对这件事有点陌生,谢谢你。
发布于 2016-01-18 01:58:02
在执行array.PushBack(...)时,array已经被移动到document中,并成为null值类型(array.IsNull() == true)。因此,您不能将PushBack设置为空值。
document["array"].PushBack(7,allocator)会工作的。
https://stackoverflow.com/questions/34748465
复制相似问题