代码之家  ›  专栏  ›  技术社区  ›  Classified

如何在Java中测试JSON Collection对象是否为空

  •  78
  • Classified  · 技术社区  · 11 年前

    我收到的JSON Collection对象如下所示:

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

    我想测试的是 problemvalue 如果 问题价值 返回一个JSON对象,我很高兴。如果没有,它将返回为 {} 。如何检测这种情况?我试了好几件事都无济于事。

    这就是我迄今为止所尝试的:

    //      if (obj.get("dps") == null) {  //didn't work
    //      if (obj.get("dps").equals("{}")) {  //didn't work
    if (obj.isNull("dps")) {  //didn't work
        System.out.println("No dps key");
    }
    

    我预计其中一行会打印“No dps key”,因为 {"dps":{}} ,但不管出于什么原因,事实并非如此。我正在使用 org.json 。jar文件是 org.json-20120521.jar .

    15 回复  |  直到 4 年前
        1
  •  173
  •   Scott Tesler    10 年前
    obj.length() == 0
    

    就是我要做的。

        2
  •  44
  •   djechlin    7 年前

    如果你能接受黑客攻击-

    obj.toString().equals("{}");
    

    序列化对象是昂贵的,对于大型对象来说更为昂贵,但最好理解JSON作为字符串是透明的,因此查看字符串表示是解决问题的方法。

        3
  •  14
  •   Rohan Lodhi    6 年前

    如果数组为空:

    .size() == 0
    

    如果对象为空:

    .length() == 0
    
        4
  •  6
  •   Anderson    11 年前

    JSON表示法{}表示一个空对象,意思是一个没有成员的对象。这与null不同。当您试图将它与字符串“{}”进行比较时,它也不是字符串。 我不知道你在使用哪个json库,但试着寻找这样的方法:

    isEmptyObject() 
    
        5
  •  5
  •   Orel Eraki    11 年前

    尝试:

    if (record.has("problemkey") && !record.isNull("problemkey")) {
        // Do something with object.
    }
    
        6
  •  2
  •   kommradHomer    10 年前

    我在JSONObject和JSONArray()上添加了isEmpty()方法

     //on JSONObject 
     public Boolean isEmpty(){         
         return !this.keys().hasNext();
     }
    

    ...

    //on JSONArray
    public Boolean isEmpty(){
        return this.length()==0;        
    }
    

    你可以在这里买到 https://github.com/kommradHomer/JSON-java

        7
  •  2
  •   sorjef    9 年前

    我会执行以下操作来检查是否有空对象

    obj.similar(new JSONObject())
    
        8
  •  2
  •   Venkata Naresh Babu    6 年前

    如果您想检查json的空情况,我们可以直接使用以下代码

    String jsonString = {};
    JSONObject jsonObject = new JSONObject(jsonString);
    if(jsonObject.isEmpty()){
     System.out.println("json is empty");
    } else{
     System.out.println("json is not empty");
    }
    

    这可能会对你有所帮助。

        9
  •  2
  •   rachit7    5 年前

    对于这种情况,我会这样做:

    var obj = {};
    
    if(Object.keys(obj).length == 0){
            console.log("The obj is null")
    }
        10
  •  0
  •   Hot Licks    11 年前
    Object getResult = obj.get("dps"); 
    if (getResult != null && getResult instanceof java.util.Map && (java.util.Map)getResult.isEmpty()) {
        handleEmptyDps(); 
    } 
    else {
        handleResult(getResult); 
    }
    
        11
  •  0
  •   Sashko mitch    8 年前

    当记录是ArrayNode时,如果JSON返回以下结构:

    {}client 
      records[]
    

    并且您想检查记录节点中是否有内容 然后可以使用size()方法来完成;

    if (recordNodes.get(i).size() != 0) {}
    
        12
  •  0
  •   Mikey    7 年前
    @Test
    public void emptyJsonParseTest() {
        JsonNode emptyJsonNode = new ObjectMapper().createObjectNode();
        Assert.assertTrue(emptyJsonNode.asText().isEmpty());
    }
    
        13
  •  0
  •   Max Ali    5 年前
    if (jsonObj != null && jsonObj.length > 0)
    

    要检查JSONObject中嵌套的JSON对象是否为空,请执行以下操作:

    if (!jsonObject.isNull("key") && jsonObject.getJSONObject("key").length() > 0)
    
        14
  •  -2
  •   michaelrmcneill    10 年前

    使用以下代码:

    if(json.isNull()!= null){  //returns true only if json is not null
    
    }
    
        15
  •  -3
  •   APerson    11 年前

    尝试 /*string with {}*/ string.trim().equalsIgnoreCase("{}")) ,也许有一些额外的空间什么的