gpt4 book ai didi

arrays - 使用 jsoncpp 迭代一组 JSON 对象

转载 作者:行者123 更新时间:2023-12-01 15:21:09 25 4
gpt4 key购买 nike

我有一个 JSON 对象数组,jsonArr 说,以下类型:

[
{ "attr1" : "somevalue",
"attr2" : "someothervalue"
},
{ "attr1" : "yetanothervalue",
"attr2" : "andsoon"
},
...
]

使用 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"]));
}
}

但不断收到错误消息
libc++abi.dylib: terminating with uncaught exception of type Json::LogicError: in Json::Value::find(key, end, found): requires objectValue or nullValue

最佳答案

很好的自我解释:

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

关于arrays - 使用 jsoncpp 迭代一组 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com