如何检查 GSON 元素的空值?
How to check for empty value for GSON element?
我有一个 JSON 数据块,如下所示:(为了便于阅读而进行了简化):
{"placeName":"Paris",
"sectionCode":"",
"elapsed":"FT",
"Time":"02/24/2015 17:30:00",
"Date":"02/24/2015 00:00:00",
"Score":"1 : 1",
"statusCode":6};
我正在使用 GSON 库(参见 https://code.google.com/p/google-gson/)以便在 java 程序中处理此 JSON。
我遇到的问题是 sectionCode 属性(上面列表中的第二个元素)。在我正在处理的其他 JSON 个数据块中,这个元素要么不存在,要么确实存在,并且包含一个整数作为其值,例如14. 这不会造成任何问题。然而,此处 sectionCode 的值只是 "".
下面是我目前用来处理 JSON 块的这段代码(其中 jsonroot
包含 JSON 数据块):
//deal with situation where no section code is provided
Integer sectioncode = null;
if ((jsonroot.get("sectionCode") != null) && (jsonroot.get("sectionCode").getAsString() != null) && (!jsonroot.get("sectionCode").isJsonNull()) && (jsonroot.get("sectionCode").getAsString() != "") && (!jsonroot.get("sectionCode").equals(null))) {
sectioncode = jsonroot.get("sectionCode").getAsInt();
}
'if' 语句中的许多条件是试图检测 sectionCode 属性值中的空字符串,因此如果是这种情况,将阻止执行以下 'getAsInt' 代码.我预计它至少会被其中之一抓住,但事实并非如此。相反,当它遇到这部分代码时,我得到以下错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.google.gson.JsonPrimitive.getAsInt(JsonPrimitive.java:260)
at MyProgram.extract_general_page_data(MyProgram.java:235)
at MyProgram.load_one_page(MyProgram.java:187)
at MyProgram.do_all(MyProgram.java:100)
at MyProgram.main(MyProgram.java:47)
我知道我可以简单地接受将要发生 NumberFormatException,并放入一个 try/catch 块来处理它,但这似乎是一种处理这种情况的混乱方法。
所以我的问题是:
为什么 'if' 语句中的任何条件都没有检测到空值?我可以用什么代替?
你有没有试过用字符串长度看它是否为零,比如这个,
if ((jsonroot.get("sectionCode") != null) && (jsonroot.get("sectionCode").getAsString().length() != 0))
{
sectioncode = jsonroot.get("sectionCode").getAsInt();
}
只需使用isJsonNull()
int sectioncode = jsonroot.get("sectionCode").isJsonNull() ? 0 : jsonroot.get("sectionCode").getAsInt();
我有一个 JSON 数据块,如下所示:(为了便于阅读而进行了简化):
{"placeName":"Paris",
"sectionCode":"",
"elapsed":"FT",
"Time":"02/24/2015 17:30:00",
"Date":"02/24/2015 00:00:00",
"Score":"1 : 1",
"statusCode":6};
我正在使用 GSON 库(参见 https://code.google.com/p/google-gson/)以便在 java 程序中处理此 JSON。
我遇到的问题是 sectionCode 属性(上面列表中的第二个元素)。在我正在处理的其他 JSON 个数据块中,这个元素要么不存在,要么确实存在,并且包含一个整数作为其值,例如14. 这不会造成任何问题。然而,此处 sectionCode 的值只是 "".
下面是我目前用来处理 JSON 块的这段代码(其中 jsonroot
包含 JSON 数据块):
//deal with situation where no section code is provided
Integer sectioncode = null;
if ((jsonroot.get("sectionCode") != null) && (jsonroot.get("sectionCode").getAsString() != null) && (!jsonroot.get("sectionCode").isJsonNull()) && (jsonroot.get("sectionCode").getAsString() != "") && (!jsonroot.get("sectionCode").equals(null))) {
sectioncode = jsonroot.get("sectionCode").getAsInt();
}
'if' 语句中的许多条件是试图检测 sectionCode 属性值中的空字符串,因此如果是这种情况,将阻止执行以下 'getAsInt' 代码.我预计它至少会被其中之一抓住,但事实并非如此。相反,当它遇到这部分代码时,我得到以下错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.google.gson.JsonPrimitive.getAsInt(JsonPrimitive.java:260)
at MyProgram.extract_general_page_data(MyProgram.java:235)
at MyProgram.load_one_page(MyProgram.java:187)
at MyProgram.do_all(MyProgram.java:100)
at MyProgram.main(MyProgram.java:47)
我知道我可以简单地接受将要发生 NumberFormatException,并放入一个 try/catch 块来处理它,但这似乎是一种处理这种情况的混乱方法。
所以我的问题是: 为什么 'if' 语句中的任何条件都没有检测到空值?我可以用什么代替?
你有没有试过用字符串长度看它是否为零,比如这个,
if ((jsonroot.get("sectionCode") != null) && (jsonroot.get("sectionCode").getAsString().length() != 0))
{
sectioncode = jsonroot.get("sectionCode").getAsInt();
}
只需使用isJsonNull()
int sectioncode = jsonroot.get("sectionCode").isJsonNull() ? 0 : jsonroot.get("sectionCode").getAsInt();