加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Json Cpp isMember()始终返回True

发布时间:2020-12-16 09:50:09 所属栏目:百科 来源:网络整理
导读:例如在简单的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 f
例如在简单的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"];

知道First Case有什么问题吗?即使我在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.

基本上,您在根[“A”]调用上创建成员“A”.为了避免这种情况,在实际访问成员之前总是检查isMember(或者只在const对象上调用它,而不是执行null检查 – 我会预先设置前者).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读