How to test if JSON object is empty in Java

前端 未结 15 2409

The JSON object I\'m receiving looks like this:

[{\"foo1\":\"bar1\", \"foo2\":\"bar2\", \"problemkey\": \"problemvalue\"}]

What I\'m trying

15条回答
  •  心在旅途
    2020-12-14 00:26

    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.

提交回复
热议问题