I have below JSON response in which I need to check whether response
field has null value or not. If response
field has null value then I need to exit out of the program.
[
{
"results": {
"response": null,
"type": "ABC"
},
"error": null
}
]
What is the easiest way to check this out? One option I know is to convert JSON to POJO and then check response field. Is there any other way?
If you are using codehouse's JSON library , you could do something like this:
JSONObject jsonObj = new JSONObject(jsonString);
System.out.println(jsonObj .isNull("error") ? " error is null ":" error is not null" );
if using Google's 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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With