简体   繁体   English

如何检查使用 Jackson 库的 JSON 是否为空或为空?

[英]How do i check if JSON using Jackson library is null or empty?

I'm trying to deserialize a Hashmap and it's breaking the program when i try to use it after.我正在尝试反序列化一个 Hashmap,当我尝试使用它时它会破坏程序。 I would like to check if the JSON is null or empty before i deserialize it.我想在反序列化之前检查 JSON 是否为空或为空。 How can i check this?我怎样才能检查这个?

    "HashMap<String, Administrador> administradores = new HashMap<>();

///here i want to check if there is something in the JSON (jackson) ///这里我想检查JSON中是否有东西(杰克逊)

    administradores = 
    Persistencia.DEserializeHashMap(Archivos.ADMINISTRADORESALL
    .getPath(), String.class, Administrador.class);///deserialize

Try-catch your code尝试捕获您的代码

try {
   administradores = Persistencia.DEserializeHashMap(Archivos.ADMINISTRADORESALL.getPath(), String.class, Administrador.class);
}
catch(Exception e) {
//It is null or empty
}    

if(administradores != null && !administradores.isEmpty()) {
       //Code
}

Personally, I prefer using com.google.gson.Gson就个人而言,我更喜欢使用com.google.gson.Gson

String json = null;
Map<String, Object> map = new Gson().fromJson(json, Map.class);
if(map != null && !map .isEmpty()) {
    //Code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM