繁体   English   中英

JsonCPP 抛出逻辑错误:需要 objectValue 或 nullValue

[英]JsonCPP throwing a logic error:requires objectValue or nullValue

void exp::example(std::string &a, std::string &b)
{   
    if (m_root.isObject() && m_root.isMember(a))
    {
        if (m_root[a].isMember(b))
        {
            m_root[a].append(b);
        }

    }
    else
    {  
        m_root[a] = Json::arrayValue;
        m_root[a].append(b);

    }
}

(m_root 在 hpp 中定义)

当我运行此代码时,出现逻辑错误:在 Json::Value::find(key, end, found) 中:需要 objectValue 或 nullValue。 我发现我从这个 if 中得到了这个错误: if (m_root[a].isMember(b))

我不明白为什么我会在那里收到此错误,我在他上面的 if 中使用了相同的 function 但我没有收到此错误。

PS function 在进入嵌套 if 之前一直工作,例如:

A b m_root
“嘿” “a1” {"hey":["a1"]}
“再见” “a2” {"hey":["a1"], "bye":["b1"]}
“你” “a3” {"hey":["a1"], "bye":["b1"], "cye":["a3"]}
“嘿” “a4” error: in Json::Value::find(key, end, found): requires objectValue or nullValue

我只在第 4 次调用时收到错误。 我感谢您的帮助!

在第 4 次迭代中,您访问类型为 arrayValue 的m_root["hey"] arrayValue isMember方法不支持这些值。

您必须以另一种方式在数组中找到值。 我建议遍历数组,例如:

bool is_inside_array(const Json::Value &json_array, const string& value_to_find) 
{
      for (const Json::Value& array_value: json_array) {
           if (array_value.asString() == value_to_find) {
                return true;
           }
      }
      return false;
}

然后将内部if替换为:

if (is_inside_array(m_root[a], b))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM