yar*_*997 27 java json hibernate extjs4
stackoverflow成员我需要你的帮助.
我在下面给出了一个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 }
Run Code Online (Sandbox Code Playgroud)
我得到parentId为null.我想将parentId值从null替换为0.
我试图用下面提到的代码来做
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);
}
Run Code Online (Sandbox Code Playgroud)
但它似乎没有奏效.我在这做错了什么.
Raj*_*ula 122
使用以下JsonObject方法检查针对任何键的值是否为null
public boolean isNull(java.lang.String key)
Run Code Online (Sandbox Code Playgroud)
此方法用于针对任何键检查Null,或者如果没有键的值.
在文档中查看
您的修改后的代码应该是这样的
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);
}
Run Code Online (Sandbox Code Playgroud)
if(jsonObject.isNull("parentId")){
jsonObject.put("parentId", 0);
}
Run Code Online (Sandbox Code Playgroud)
对于 com.google.gson.JsonObject,我按照以下步骤操作:
boolean isIdNull = jsonObject.get("Id").isJsonNull();
Run Code Online (Sandbox Code Playgroud)
在我的 json 中,我有:
"Id":null
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73065 次 |
| 最近记录: |