This is my Json array.
{
"colorsArray":[{
"colorName":"red",
"hexValue":"#f00"
},
{
"colorName":"green",
"hexValue":"#0f0"
},
{
"colorName":"blue",
"hexValue":"#00f"
}
]
}
I want to get value 'green'. My c++ code in rapidjson library is
Document document;
document.Parse<0>(jsoncontent.c_str()).HasParseError();
document.Parse(jsoncontent.c_str());
const Value& user = document["colorsArray"];
string name = user["colorName"].GetString();
When I try to access the colorName, I am getting the below Runtime error.
rapidjson::GenericValue<Encoding, Allocator>::MemberIterator
rapidjson::GenericValue<Encoding, Allocator>::FindMember(const
rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator
= rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>;
Allocator = rapidjson::MemoryPoolAllocator<>;
rapidjson::GenericValue<Encoding, Allocator>::MemberIterator =
rapidjson::GenericMemberIterator<false, rapidjson::UTF8<>,
rapidjson::MemoryPoolAllocator<> >]: Assertion `IsObject()' failed.
Aborted (core dumped)
How can I read a particular value using Rapidjson Library ?
Briefly, in my opinion, the document is incomplete. If you put the reading-value code in a loop body or a thread and run the program, the error would happen when you terminate. So, judge the document is IsObject() first when you want to get a value.
Maybe this can be work:
Document document;
document.Parse<0>(jsoncontent.c_str()).HasParseError();
document.Parse(jsoncontent.c_str());
if(document.IsObject()) // add this line
{
const Value& user = document["colorsArray"];
string name = user["colorName"].GetString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.