Fastjson JSONObject 没有 has 方法?使用 containsKey 代替!
在阿里巴巴提供的fastjson库中,JSONObject确实没有has方法。如果您想检查一个JSON对象是否包含某个属性,您可以使用containsKey方法。以下是一个示例:
import com.alibaba.fastjson.JSONObject;
public class Main {
public static void main(String[] args) {
String jsonString = "{\"name\":\"John\", \"age\":30}";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
if (jsonObject.containsKey("name")) {
System.out.println("JSON对象包含属性name");
} else {
System.out.println("JSON对象不包含属性name");
}
}
}
这将输出"JSON对象包含属性name",因为JSON对象中包含名为"name"的属性。
原文地址: https://www.cveoy.top/t/topic/pS3s 著作权归作者所有。请勿转载和采集!