gpt4 book ai didi

c++ - 如何使用nlohmann检查c++中嵌套json中是否存在 key

转载 作者:太空狗 更新时间:2023-10-29 22:54:16 25 4
gpt4 key购买 nike

我有以下 json 数据:

{
"images": [
{
"candidates": [
{
"confidence": 0.80836,
"enrollment_timestamp": "20190613123728",
"face_id": "871b7d6e8bb6439a827",
"subject_id": "1"
}
],
"transaction": {
"confidence": 0.80836,
"enrollment_timestamp": "20190613123728",
"eyeDistance": 111,
"face_id": "871b7d6e8bb6439a827",
"gallery_name": "development",
"height": 325,
"pitch": 8,
"quality": -4,
"roll": 3,
"status": "success",
"subject_id": "1",
"topLeftX": 21,
"topLeftY": 36,
"width": 263,
"yaw": -34
}
}
]
}

我需要检查上面的 json 数据中是否存在 subject_id。为此,我在下面做了:

auto subjectIdIter = response.find("subject_id");
if (subjectIdIter != response.end())
{
cout << "it is found" << endl;

}
else
{
cout << "not found " << endl;
}

我该如何解决这个问题。谢谢

最佳答案

有一个成员函数contains返回一个 bool 指示给定键是否存在于 JSON 值中。

代替

auto subjectIdIter = response.find("subject_id");
if (subjectIdIter != response.end())
{
cout << "it is found" << endl;

}
else
{
cout << "not found " << endl;
}

你可以这样写:

if (response.contains("subject_id")
{
cout << "it is found" << endl;

}
else
{
cout << "not found " << endl;
}

关于c++ - 如何使用nlohmann检查c++中嵌套json中是否存在 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648174/

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