jsonObject.isNull(key) // key 字符串 判断文件中的数据是否为空。
JSONObject中JSONObject.isNull(name)配合if语句与逻辑运算符可以避免调用getString(name)时他是个空的对象
判断是否存在jsonObject中是否存在某个对象
jsonObject.isNull("name");
从文件读取JSON判断null(增加程序健壮性)
实际开发中读取JSON数据是在WEB环境下(网络上提供的API或者文件中)
如果在读取时,JSON数据中没有指定key,一般需要打印一个日志或者抛出一个异常,这里可以使用JSONObject对象的isNull("key")方法,判断如果没有key值做一些操作。

isNull
jsonObject.isNull(key) // key 字符串 判断文件中的数据是否为空。
jsonObject.isNull("name")
//判断json对象中这个属性是否为空,存在返回true
判断Json数据中是否有指定的信息 JSONObject jsonObject = new JSONObject(content); if (!jsonObject.isNull("name")) System.out.println("姓名:" + jsonObject.getString("name")); if (!jsonObject.isNull("age")) System.out.println("年龄:" + jsonObject.getDouble("age")); if (!jsonObject.isNull("birthday")) System.out.println("生日:" + jsonObject.getString("birthday")); if (!jsonObject.isNull("school")) System.out.println("学校:" + jsonObject.getString("school"));
isNull属性判断json数据中是否有该对象属性