我有JSON响应,我需要检查response字段是否有空值.如果response字段具有空值,那么我需要退出程序.
[
{
"results": {
"response": null,
"type": "ABC"
},
"error": null
}
]
Run Code Online (Sandbox Code Playgroud)
检查这个的最简单方法是什么?我知道的一个选项是将JSON转换为POJO,然后检查响应字段.还有其他方法吗?
如果您使用的是codehouse的JSON库,则可以执行以下操作:
JSONObject jsonObj = new JSONObject(jsonString);
System.out.println(jsonObj .isNull("error") ? " error is null ":" error is not null" );
Run Code Online (Sandbox Code Playgroud)
如果使用谷歌的gson:
JsonObject jsonObject = new JsonParser().parse(st).getAsJsonObject();
JsonElement el = jsonObject.get("error");
if (el != null && !el.isJsonNull()){
System.out.println (" not null");
}else{
System.out.println (" is null");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10442 次 |
| 最近记录: |