如何检查使用Jackson库的JSON是空的还是空的?

发布于 2025-02-06 11:57:42 字数 398 浏览 1 评论 0原文

我正在尝试对哈希图进行挑选,并且当我尝试使用它时,它正在破坏程序。我想检查json是空的还是空的,然后才能对其进行审理。我该如何检查?

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

///在这里我想检查JSON(Jackson)中是否有东西

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一枫情书 2025-02-13 11:57:43

亲自试用您的代码

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
}

,我更喜欢使用com.google.gson.gson.gson

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文