【发布时间】:2016-06-23 08:08:54
【问题描述】:
我正在使用以下代码创建一个JSONObject,但变量即使在初始化后仍显示null:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", "pradyut");
jsonObject.put("password", "5F4DCC3B5AA765D61D8327DEB882CF99");
} catch (JSONException e) {
e.printStackTrace();
Log.i("log", "json exception : " + e.toString());
}
if (jsonObject == null) {
Log.i("log", "the json object is not null");
// this part is showing everytime.
} else {
Log.i("log", "the json object is null");
}
我没有遇到任何异常。
为什么会这样?
【问题讨论】:
-
你的 if 语句条件错误。 == null 表示它正在检查它是否为空。
-
"但变量在初始化后仍显示为 null" 不,不是。如果您说您看到消息
the json object is null,那是因为您的if落后了。但是你不需要if;在那段代码中,jsonObject根本不可能是null。 -
@intj:是的,希望他能做到,而不是反复重申“我做的一切都是对的”。
-
@T.J.Crowder 如果他不这样做,则可以将其标记为拼写错误并进行标记。
-
@intj:是的,我投了同样的票。这太糟糕了。显然存在一些问题,但如果信息不存在,我们将无能为力......