c ++ rapidjson 获取添加值到现有数组
c++ rapidjson get add value to exist array
我试图创建一个 class,它将向 Json 数组添加一个值。我试着这样做:
void functions::Json::SetArray(rapidjson::Document &JsonObj, std::string ArrayName, std::string value)
{
if (!JsonObj.IsObject())
JsonObj.SetObject();
rapidjson::Document::AllocatorType& alloc = JsonObj.GetAllocator();
rapidjson::Value KeyPart;
KeyPart.SetString(ArrayName.c_str(), alloc);
rapidjson::Value ValuePart;
ValuePart.SetString(value.c_str(), alloc);
rapidjson::Value array(rapidjson::kArrayType);
if (!JsonObj["array"].IsNull())
array = JsonObj["array"];
array.PushBack(ValuePart, alloc);
JsonObj.AddMember(KeyPart, array, alloc);
}
但是我有一个错误:
rapidjson/document.h:1218: rapidjson::GenericValue<Encoding,
Allocator>& rapidjson::GenericValue<Encoding,
Allocator>::operator[](const rapidjson::GenericValue<Encoding,
SourceAllocator>&) [with SourceAllocator =
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>; Encoding =
rapidjson::UTF8<>; Allocator =
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>]: Assertion
`false' failed.
我该如何正确操作?
看看 rapidjson/document.h:1218
,这似乎是当您将 []
运算符与不存在的成员一起使用时出错的原因,可能是您访问 JsonObj["array"]
的地方.
如果我先使用 MemberFind
查找成员,它对我来说没问题:
rapidjson::Document::MemberIterator existingArray = JsonObj.FindMember("array");
if (existingArray != JsonObj.MemberEnd() && !existingArray->value.IsNull())
array = existingArray->value;
我试图创建一个 class,它将向 Json 数组添加一个值。我试着这样做:
void functions::Json::SetArray(rapidjson::Document &JsonObj, std::string ArrayName, std::string value)
{
if (!JsonObj.IsObject())
JsonObj.SetObject();
rapidjson::Document::AllocatorType& alloc = JsonObj.GetAllocator();
rapidjson::Value KeyPart;
KeyPart.SetString(ArrayName.c_str(), alloc);
rapidjson::Value ValuePart;
ValuePart.SetString(value.c_str(), alloc);
rapidjson::Value array(rapidjson::kArrayType);
if (!JsonObj["array"].IsNull())
array = JsonObj["array"];
array.PushBack(ValuePart, alloc);
JsonObj.AddMember(KeyPart, array, alloc);
}
但是我有一个错误:
rapidjson/document.h:1218: rapidjson::GenericValue<Encoding,
Allocator>& rapidjson::GenericValue<Encoding,
Allocator>::operator[](const rapidjson::GenericValue<Encoding,
SourceAllocator>&) [with SourceAllocator =
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>; Encoding =
rapidjson::UTF8<>; Allocator =
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>]: Assertion
`false' failed.
我该如何正确操作?
看看 rapidjson/document.h:1218
,这似乎是当您将 []
运算符与不存在的成员一起使用时出错的原因,可能是您访问 JsonObj["array"]
的地方.
如果我先使用 MemberFind
查找成员,它对我来说没问题:
rapidjson::Document::MemberIterator existingArray = JsonObj.FindMember("array");
if (existingArray != JsonObj.MemberEnd() && !existingArray->value.IsNull())
array = existingArray->value;