Rapidjson解析数组

时间:2017-02-12 13:01:44

标签: c++ rapidjson

我正在尝试使用rapidjson库来解析带有对象数组的json字符串,如下例所示,但是一旦我引用了“docs”元素,即使“assert(IsArray)”正常,它也是如此当我尝试迭代数组时失败,因为当我调用函数“Size()”时它失败了。

{

"response":{"docs":[
      {
        "eircode":"D02 YN32",
        "loc":"53.3440763,-6.2356984",
        "addr":"ACCENTURE, 7 HANOVER QUAY, DUBLIN 2",
        "geodist()":462.40406697863784
      },
      {
        "eircode":"N37 Y985",
        "loc":"53.4243401,-7.9216913",
        "addr":"ATHLONE COMMUNITY SCHOOL, RETREAT ROAD, ATHLONE, CO. WESTMEATH",
        "geodist()":569.0322899541842}
      }
      ]

}

这是我的C ++代码:

const Value& response = d["response"];
assert(response.IsObject());

assert(response.HasMember("docs"));

const Value& docs_list = response["docs"];
assert(docs_list.IsArray());

for (int i=0; i<docs_list.Size(); i++)
{
    //something here
}

我得到的错误:

rapidjson/document.h:1128: rapidjson::GenericValue<Encoding, Allocator>::ConstMemberIterator rapidjson::GenericValue<Encoding, Allocator>::MemberEnd() const [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::GenericValue<Encoding, Allocator>::ConstMemberIterator = rapidjson::GenericMemberIterator<true, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> >]: Assertion 'IsObject()' failed. Aborted

有人可以帮我这个吗?感谢。

0 个答案:

没有答案