如何在java中比较JsonObject的空值

时间:2021-07-08 19:40:14

* member i need some help from you.

*成员我需要你的帮助。

I am having a JsonObject given below

下面给出了一个JsonObject

{
"Id": null,
"Name": "New Task",
"StartDate": "2010-03-05T00:00:00",
"EndDate": "2010-03-06T00:00:00",
"Duration": 1,
"DurationUnit": "d",
"PercentDone": 60,
"ManuallyScheduled": false,
"Priority": 1,
"parentId": null,
"index": 2,
"depth": 1,
"checked": null }

i am getting parentId as null. I want to replace the parentId value from null to 0.

我将parentId设为空。我想要将parentId值从null替换为0。

I am trying to do it with below mentioned code

我正在尝试使用下面提到的代码

if(jsonObject.get("parentId") == null || jsonObject.get("parentId") == "")
    {
        System.out.println("inside null");
        jsonObject.put("parentId", 0);
    }
    else
    {
        System.out.println("inside else part");
        //jsonObject.put("parentId", jsonObject.getInt("parentId"));
        jsonObject.put("parentId", 0);
    }

but it seems not to be working. What I am doing wrong here.

但这似乎行不通。我做错了什么。

4 个解决方案

#1


105  

Use the following method of JsonObject to check if a value against any key is null

使用JsonObject的以下方法检查针对任何键的值是否为null

public boolean isNull(java.lang.String key)

This method is used to check Null against any key or if there is no value for the key.

此方法用于针对任何键检查Null或该键是否没有值。

check this in the documentation

在文档中检查一下

Your Modified code should be like this

修改后的代码应该是这样的。

if(jsonObject.isNull("parentId"))
    {
        System.out.println("inside null");
        jsonObject.put("parentId", 0);
    }
    else
    {
        System.out.println("inside else part");
        //jsonObject.put("parentId", jsonObject.getInt("parentId"));
        jsonObject.put("parentId", 0);
    }

#2


3  

if(jsonObject.isNull("parentId")){
    jsonObject.put("parentId", 0);
}

#3


2  

For com.google.gson.JsonObject, I followed this :

com.google.gson。JsonObject,我是这么说的

boolean isIdNull = jsonObject.get("Id").isJsonNull();

In my json, I have :

在json中,我有:

"Id":null

#4


0  

Try the following codes.

尝试以下代码。

if(jsonObject.isNull("parentId") || jsonObject.get("parentId").equals(""))

#1


105  

Use the following method of JsonObject to check if a value against any key is null

使用JsonObject的以下方法检查针对任何键的值是否为null

public boolean isNull(java.lang.String key)

This method is used to check Null against any key or if there is no value for the key.

此方法用于针对任何键检查Null或该键是否没有值。

check this in the documentation

在文档中检查一下

Your Modified code should be like this

修改后的代码应该是这样的。

if(jsonObject.isNull("parentId"))
    {
        System.out.println("inside null");
        jsonObject.put("parentId", 0);
    }
    else
    {
        System.out.println("inside else part");
        //jsonObject.put("parentId", jsonObject.getInt("parentId"));
        jsonObject.put("parentId", 0);
    }

#2


3  

if(jsonObject.isNull("parentId")){
    jsonObject.put("parentId", 0);
}

#3


2  

For com.google.gson.JsonObject, I followed this :

com.google.gson。JsonObject,我是这么说的

boolean isIdNull = jsonObject.get("Id").isJsonNull();

In my json, I have :

在json中,我有:

"Id":null

#4


0  

Try the following codes.

尝试以下代码。

if(jsonObject.isNull("parentId") || jsonObject.get("parentId").equals(""))