做项目时,遇到这么一个坑:如何判断一上json 是否为空, 开始的代码是这样的:
JSONObject detail = JSONObject.fromObject(
"123");
if (detail ==
null)
{
detail =
new JSONObject();
}
12345
12345
发现有问题,无论怎么调整字符串的内容,都不会走 new Jsonobject() 。各种试,各种问,最后没办法了,拿detail 的方法一个个试,才发现居然有 isNullObject() 这样一个方法,于是有了下面的代码,
JSONObject detail = JSONObject.fromObject(
"123");
if (detail ==
null || detail.isNullObject())
{
detail =
new JSONObject();
}
12345
12345
这么长时间,各种坑都踩,以后还会有坑,解决方案: 1. 网上找下有没有类似的问题; 2. 像今天这种情况,类可能已经提供了可用方法,试其中的方法; 3. 同事是很好的老师,多问,面子啥的在这晨最不值钱了。 感谢同事
转载请注明原文地址: https://ju.6miu.com/read-8732.html