简体   繁体   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 is defind in the hpp) (m_root 在 hpp 中定义)

When I'm running this code I get the logic error: in Json::Value::find(key, end, found): requires objectValue or nullValue.当我运行此代码时,出现逻辑错误:在 Json::Value::find(key, end, found) 中:需要 objectValue 或 nullValue。 I found out that I got this error from this if: if (m_root[a].isMember(b))我发现我从这个 if 中得到了这个错误: if (m_root[a].isMember(b))

I don't understand why do I get this error there, I used the same function in the if above him and I didn't get this error.我不明白为什么我会在那里收到此错误,我在他上面的 if 中使用了相同的 function 但我没有收到此错误。

PS the function is working until it enter the nested if, example: PS function 在进入嵌套 if 之前一直工作,例如:

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

I get the error only in the 4th call.我只在第 4 次调用时收到错误。 I appreciate your help!我感谢您的帮助!

On the 4th iteration you access the m_root["hey"] object which is of type arrayValue .在第 4 次迭代中,您访问类型为 arrayValue 的m_root["hey"] arrayValue Those values are not supported by the isMember method. isMember方法不支持这些值。

You'll have to find the value in the array in another way.您必须以另一种方式在数组中找到值。 I suggest iterating over the array, in something like:我建议遍历数组,例如:

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;
}

Then replace the inner if with:然后将内部if替换为:

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

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

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