JSONCPP 태그가 NULL인지 확인하는 방법 .ISNULL () 던지기 어설 션
-
10-12-2019 - |
문제
IM JSONCPP를 사용하는 IM, json 구조에 태그가 포함되어 있는지 확인해야합니다. 내가 할 때 :
UserRoot0["error"].isNull()
.
json_value.cpp line 1025 에서 저를 던졌습니다.
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
.
응답 IM 이이 유형에서 얻는지 확인하고 싶습니다 :
{
"error" : {
"message" : "Error validating application.",
"type" : "OAuthException",
"code" : 190
}
}
. 해결책
The []
operator is only valid for JsonValue
objects that are of type Object
or null. All others (Int
, Bool
, Array
, etc.) will assert.
If your UserRoot0
object is an Array
or some other non-Object
type, you have some more work to do (like iterating into sub-nodes) to find your target node that may or may not contain the error. Print UserRoot0.toStyledString()
to see what your JSON looks like, and make sure it looks like a JSON Object (see json.org for a nice overview of what that is).
A "ToDo" comment at the top of the json_value.cpp source file (where JSON_ASSERT
is defined) implies that the developers may be planning more robust error handling instead of these asserts in future versions, but in the meantime, you can check yourself, like this:
if(UserRoot0.isObject() && UserRoot0.isMember("error"))
// Process error node
else
// This node isn't an Object node or doesn't contain the "error" key
The isMember()
check will also assert for non-Object
nodes, so be sure to check isObject()
before checking isMember()
if UserRoot0
isn't guaranteed to be an Object
.
다른 팁
I stumbled on this too. As Ennael says you need to make sure you are dealing with an object type. FWIW my problem was caused by using JSON like this:
"error" : {
"code" : 190
}
... instead of what I intended:
{
"error" : {
"code" : 190
}
}
Excluding the outer set of parenthesis will cause the value type to become string instead of object.