gpt4 book ai didi

c++ - Json Cpp isMember() 总是返回 True

转载 作者:太空狗 更新时间:2023-10-29 20:13:27 28 4
gpt4 key购买 nike

例如在一个简单的 json 中

{
"A" :
{
"B" :
{
--something--
}
}
}

第一种情况:

json::Value root;
const Json::Value x = root["A"]["B"];
if (root.isMember("A")) --- always returns TRUE..

第二种情况:

Json::Value root;
If (root.isMember("A")) ---- works fine
const Json::Value x = root["A"]["B"];

知道第一个案例有什么问题吗?即使我在 isMember() 调用之前得到了 x

最佳答案

看看documentation .

Value &     operator[] (const char *key)
Access an object value by name, create a null member if it does not exist.
const Value & operator[] (const char *key) const
Access an object value by name, returns null if there is no member with that name.
Value & operator[] (const std::string &key)
Access an object value by name, create a null member if it does not exist.
const Value & operator[] (const std::string &key) const
Access an object value by name, returns null if there is no member with that name.

基本上,您是在 root["A"] 调用上创建成员 "A"。为避免这种情况,请始终在实际访问成员之前检查 isMember(或仅在 const 对象上调用它并进行空检查 - 我更喜欢前者)。

关于c++ - Json Cpp isMember() 总是返回 True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21346874/

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