Json::Value 类型的 jsoncpp 编译错误

jsoncpp compilation error for Json::Value type

这是架构:

{
"definitions": {
    "properties": {
        "Count": {
            "type": [
                "number",
                "null"
            ]
        }
    }
}

} 我想阅读 "type"

中的成员

我尝试了很多方式,例如

if(val["definitions"]["properties"]["Count"]["type"][0] == "number" and 
(val["definitions"]["properties"]["Count"]["type"][1] == "null"))
{
    //code here
}

这会导致以下错误

error : terminate called after throwing an instance of 'Json::LogicError' what(): in Json::Value::operator: requires arrayValue Aborted (core dumped)

而对于这段代码

if (val["definitions"]["properties"]["Count"]["type"][0].isMember("number") and 
(val["definitions"]["properties"]["Count"]["type"][0].isMember("null"))){
    //code here
}

我明白了

error: terminate called after throwing an instance of 'Json::LogicError' what(): in Json::Value::find(key, end, found): requires objectValue or nullValue Aborted (core dumped)

你的代码是正确的......你的 json 是错误的......JsonCpp 需要 "definitions" 的引号并且你不能在数组或对象的最后一项之后有额外的逗号.

只需将其更改为:

{
    "definitions": {
        "properties": {
            "Count": {
                "type": [
                    "number",
                    "null"
                ]
            }
        }
    }
}

;-)

 const Json::Value obj=val["definitions"]["properties"]["count"]["type"];
    if (std::find(obj.begin(),obj.end(),"string")!=obj.end() and 
 std::find(obj.begin(),obj.end(),"null")!=obj.end()){
    // code here;
    }