使用jsoncpp迭代一组JSON对象

Joh*_* S. 2 arrays json jsoncpp c++11

我有一个JSON对象数组,jsonArr说,有以下几种:

[
  { "attr1" : "somevalue",
    "attr2" : "someothervalue"
  },
  { "attr1" : "yetanothervalue",
    "attr2" : "andsoon"
  },
  ...
]
Run Code Online (Sandbox Code Playgroud)

使用jsoncpp,我试图遍历数组并检查每个对象是否有成员"attr1",在这种情况下我想将相应的值存储在向量中values.

我尝试过类似的东西

Json::Value root;
Json::Reader reader;
Json::FastWriter fastWriter;
reader.parse(jsonArr, root);

std::vector<std::string> values;

for (Json::Value::iterator it=root.begin(); it!=root.end(); ++it) {
  if (it->isMember(std::string("attr1"))) {
    values.push_back(fastWriter.write((*it)["uuid"]));
  }
}
Run Code Online (Sandbox Code Playgroud)

但不断收到错误消息

libc++abi.dylib: terminating with uncaught exception of type Json::LogicError: in Json::Value::find(key, end, found): requires objectValue or nullValue
Run Code Online (Sandbox Code Playgroud)

Sga*_*Sga 8

非常自我解释:

for (Json::Value::ArrayIndex i = 0; i != root.size(); i++)
    if (root[i].isMember("attr1"))
        values.push_back(root[i]["attr1"].asString());
Run Code Online (Sandbox Code Playgroud)