stackoverflow member i need some help from you.

I am having a JsonObject given below

{
"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 }

我得到的parentId为null.我想将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.

推荐答案

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

public boolean isNull(java.lang.String key)

此方法用于根据任何键判断Null,或者判断键是否没有值.

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);
    }

Json相关问答推荐

JOLT将对象名作为新属性添加到主体中

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

如何使用PlayWriter循环访问JSON对象

在ConvertFrom-Json之后需要从PowerShell对象中获取数据

(Kotlin)com.google.gson.internal.LinkedTreeMap无法转换为com.example.phonetest2.model.HallData

如何使用jolt将嵌套数据变成线性数据

jq - 将父键值提取为子元素旁边的逗号分隔值

小写嵌套特定键的震动转换

为什么根据其他工具,来自 aws rds 的 JSON 在 Docker 中格式错误运行?

如何在 powerapps 中将详细信息表单转换为 json?

JQ 中的样本标准偏差

根据数据框中的其他列值将列表 json 对象插入行

在 postgresql 中将行转换为 json 对象

如何让 JSON.NET 忽略对象关系?

UTF-8 字符编码之战 json_encode()

如何使用 gson 将数据保存在 json 文件中?

如何使用 Jackson 定义可选的 json 字段

Android JSON 库的性能和可用性比较

将 JSON 对象推送到 localStorage 中的数组

Jsonpath 与 Jackson 或 Gson