【问题标题】:JsonCPP throwing a logic error:requires objectValue or nullValueJsonCPP 抛出逻辑错误:requires objectValue or nullValue
【发布时间】:2021-10-02 12:04:13
【问题描述】:
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 (m_root[a].isMember(b))

我不明白为什么会出现这个错误,我在他上面的 if 中使用了相同的函数,我没有得到这个错误。

P.S 该函数一直在工作,直到它进入嵌套的 if,例如:

a b m_root
"hey" "a1" {"hey":["a1"]}
"bye" "a2" {"hey":["a1"], "bye":["b1"]}
"cye" "a3" {"hey":["a1"], "bye":["b1"], "cye":["a3"]}
"hey" "a4" error: in Json::Value::find(key, end, found): requires objectValue or nullValue

我只在第 4 次通话中收到错误。 感谢您的帮助!

【问题讨论】:

    标签: c++ json jsoncpp


    【解决方案1】:

    在第 4 次迭代中,您访问类型为 arrayValue 的 m_root["hey"] 对象。 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))
    

    【讨论】:

    • 有没有办法在不遍历数组的情况下做到这一点?某种 JsonCPP 函数?
    • 从here 中写的我猜答案是否定的:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2019-11-10
    • 2019-01-14
    • 2013-06-30
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多