简体   繁体   English

Java无法识别jSonObject中的空值

[英]Java not recognizing null value in jSonObject

I have a jSonObject with key values pairs and i want to parse it to ContentValues. 我有一个具有键值对的jSonObject,我想将其解析为ContentValues。 During the parse i want to detect wether the value is null, as you can see in the picture the value is null, but still it does not get catch by the if statement. 在解析期间,我想检测该值是否为null,如您在图片中所见,该值为null,但仍然不能被if语句捕获。 Because it's a json and might be pasing null as string i tried to use value.equals("null") but neither it worked. 因为它是一个json,并且可能将null用作字符串,所以我尝试使用value.equals(“ null”),但均无效。

调试信息

if ( value == null | value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}

What's wrong ? 怎么了 ?

Your first "OR" is just a single pipe '|' 您的第一个“ OR”只是单个管道“ |” instead of the double-pipe '||' 而不是双管道'||'

if ( value == null || value.equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}

Try it 试试吧

if ( value == null || value.equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}

Try using value.isNullObject() function. 尝试使用value.isNullObject()函数。 It returns true if the object is NULL. 如果对象为NULL,则返回true。

if ( value == null | value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}

Though in your case if value is "null" above will turn out to be 尽管在您的情况下,如果值是“ null”,则上述结果将是

value == null | value. equals("null")// first condition in if statement --->if(false|true)---> if(true)

you are using bit wise OR not logical OR 您正在使用按位或非逻辑或

It should be 它应该是

  if ( value == null || value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
        DO SOME CODE
    }

The common way to retrieve values from jsonobject is using getString() method. 从jsonobject检索值的常用方法是使用getString()方法。 So you must use value = jsonObject.getString(columnName); 因此,您必须使用value = jsonObject.getString(columnName); to assign the value for value variable. value变量分配 Then you need to check the condition as 然后您需要检查条件为

if ( value == null || value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) 
{

}

You're missing one | 您缺少一个| in you first condition 在你的第一个条件

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

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