How to test if JSON object is empty in Java

前端 未结 15 2286

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

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

What I\'m trying

相关标签:
15条回答
  • 2020-12-14 00:13
    if (jsonObj != null && jsonObj.length > 0)
    

    To check if a nested JSON object is empty within a JSONObject:

    if (!jsonObject.isNull("key") && jsonObject.getJSONObject("key").length() > 0)
    
    0 讨论(0)
  • 2020-12-14 00:13

    Use the following code:

    if(json.isNull()!= null){  //returns true only if json is not null
    
    }
    
    0 讨论(0)
  • 2020-12-14 00:20

    Try:

    if (record.has("problemkey") && !record.isNull("problemkey")) {
        // Do something with object.
    }
    
    0 讨论(0)
  • 2020-12-14 00:20
    @Test
    public void emptyJsonParseTest() {
        JsonNode emptyJsonNode = new ObjectMapper().createObjectNode();
        Assert.assertTrue(emptyJsonNode.asText().isEmpty());
    }
    
    0 讨论(0)
  • 2020-12-14 00:24

    Try /*string with {}*/ string.trim().equalsIgnoreCase("{}")), maybe there is some extra spaces or something

    0 讨论(0)
  • 2020-12-14 00:25

    For this case, I do something like this:

    var obj = {};
    
    if(Object.keys(obj).length == 0){
            console.log("The obj is null")
    }

    0 讨论(0)
提交回复
热议问题