简体   繁体   中英

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. I would like to check if the JSON is null or empty before i deserialize it. How can i check this?

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

///here i want to check if there is something in the JSON (jackson)

    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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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