The JSON object I\'m receiving looks like this:
[{\"foo1\":\"bar1\", \"foo2\":\"bar2\", \"problemkey\": \"problemvalue\"}]
What I\'m trying
obj.length() == 0
is what I would do.
if you want to check for the json empty case, we can directly use below code
String jsonString = {};
JSONObject jsonObject = new JSONObject(jsonString);
if(jsonObject.isEmpty()){
System.out.println("json is empty");
} else{
System.out.println("json is not empty");
}
this may help you.
A JSON notation {} represents an empty object, meaning an object without members. This is not the same as null. Neither it is string as you are trying to compare it with string "{}". I don't know which json library are you using, but try to look for method something like:
isEmptyObject()
If JSON returned with following structure when records is an ArrayNode:
{}client
records[]
and you want to check if records node has something in it then you can do it using a method size();
if (recordNodes.get(i).size() != 0) {}
I would do the following to check for an empty object
obj.similar(new JSONObject())
Object getResult = obj.get("dps");
if (getResult != null && getResult instanceof java.util.Map && (java.util.Map)getResult.isEmpty()) {
handleEmptyDps();
}
else {
handleResult(getResult);
}