简体   繁体   中英

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)

When I'm running this code I get the logic error: in Json::Value::find(key, end, found): requires objectValue or nullValue. I found out that I got this error from this 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.

PS the function is working until it enter the nested if, example:

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

I get the error only in the 4th call. I appreciate your help!

On the 4th iteration you access the m_root["hey"] object which is of type arrayValue . Those values are not supported by the isMember method.

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 (is_inside_array(m_root[a], b))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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