hutool 判断 JSONObject 对象是否有指定键名

在 Hutool 中,可以使用 JSONObject 的 has 方法来判断一个 JSONObject 是否包含指定的键。以下是一个示例代码,用于判断 JSONObject 是否包含键名为 "name" 的值。

java 复制代码
import cn.hutool.json.JSONObject;  
  
public class Example {  
    public static void main(String[] args) {  
        JSONObject jsonObject = new JSONObject();  
        jsonObject.put("name", "John");  
        jsonObject.put("age", 30);  
  
        if (jsonObject.has("name")) {  
            System.out.println("JSONObject 包含键名为 name 的值");  
        } else {  
            System.out.println("JSONObject 不包含键名为 name 的值");  
        }  
    }  
}

在上面的示例中,我们首先创建了一个 JSONObject 对象,并添加了两个键值对,其中一个键的名称是 "name"。然后,我们使用 has 方法检查该对象是否包含键名为 "name" 的值。如果包含,则输出 "JSONObject 包含键名为 name 的值";否则,输出 "JSONObject 不包含键名为 name 的值"。

相关推荐
钱栈up4 分钟前
qoder CLI 1.0.45 本地小说生成工作流搭建:Feature Gate 配置与长文本断连排查从死锁到全流程跑通:Python德州扑克项目的优化
java·前端·数据库
代码调试师8 分钟前
【毕设分享】springboot攀枝花生鲜电商平台58990
java·vue.js·spring boot·后端·架构·eclipse·课程设计
xcl092510 分钟前
全民健身解决方案小程序系统开发实战:从架构设计到上线指南
java·spring boot
hiahiahia12312 分钟前
为什么 Structured Output 会变成 Agent 的基础设施?
前端·人工智能
qiuziqiqi12 分钟前
cf前端直接上传
前端
洛阳泰山16 分钟前
别卷 Python 了:我用 Java 21 + Spring Boot 3 打造了一个企业级 RAG + 智能体工作流引擎(附架构与源码解析)
java·spring boot·后端
网硕互联的小客服20 分钟前
服务器可以远程连接,但是为什么无法Ping通?如何解决处理?
运维·服务器
杨运交21 分钟前
[070][Web模块]Spring MVC TOTP 二次认证拦截器:设计与源码深度解析
前端·spring·mvc
MetaLite25 分钟前
Spring Cache只留注解-MetaLite如何二开缓存运行时
java·spring·缓存
Wang's Blog30 分钟前
Java框架快速入门: Spring Security+OAuth2之自定义数据库表结构实现认证
java·数据库·spring