即使Json是正确的,也要获取Gson NumberFormatExcepption
{
"IsMissCallLogin": true,
"IsOfficialSurname": true,
"IsGotraInRegRequest": false,
"SubCasteLabel": "SubCaste",
"MissCallWaitSeconds": 7,
"IsFBAccountKit": true,
"Status": 1,
"Message": "Success",
"Navigation": "KeepSame"
}
使用Gson gradle作为
compile 'com.google.code.gson:gson:2.7'
例外是
NumberFormatException : Thread:main
Exception:com.google.gson.JsonSyntaxException:
java.lang.NumberFormatException: empty String at
com.google.gson.internal.a.s$34.b(Unknown Source) at
com.google.gson.internal.a.s$34.a(Unknown Source) at
com.google.gson.internal.a.k$1.a(Unknown Source) at
com.google.gson.internal.a.l.a(Unknown Source) at
com.google.gson.d.a(Unknown Source) at
com.google.gson.d.a(Unknown Source) at
com.google.gson.d.a(Unknown Source)
模型类
public class GetUIConfigResponse {
public boolean IsMissCallLogin;
public boolean IsOfficialSurname;
public boolean IsGotraInRegRequest;
public String SubCasteLabel;
public int MissCallWaitSeconds;
public boolean IsFBAccountKit;
public int Status;
public String Message;
public String Navigation;
public GetUIConfigResponse() {
IsMissCallLogin = true;
IsOfficialSurname = true;
IsGotraInRegRequest = false;
SubCasteLabel = "SubCaste";
Status = 0;
Message = "";
Navigation = "";
MissCallWaitSeconds = 5;
IsFBAccountKit = false;
}
}
答案 0 :(得分:0)
这是你在使用Gson时应该如何制作你的pojo课程。提供的注释很好用,也很方便。
public class GroupMemberListingResponse {
@SerializedName("IsMissCallLogin")
@Expose
private boolean isMissCallLogin;
@SerializedName("IsOfficialSurname")
@Expose
private boolean isOfficialSurname;
@SerializedName("IsGotraInRegRequest")
@Expose
private boolean isGotraInRegRequest;
@SerializedName("SubCasteLabel")
@Expose
private String subCasteLabel;
@SerializedName("MissCallWaitSeconds")
@Expose
private int missCallWaitSeconds;
@SerializedName("IsFBAccountKit")
@Expose
private boolean isFBAccountKit;
@SerializedName("Status")
@Expose
private int status;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("Navigation")
@Expose
private String navigation;
public boolean isIsMissCallLogin() {
return isMissCallLogin;
}
public void setIsMissCallLogin(boolean isMissCallLogin) {
this.isMissCallLogin = isMissCallLogin;
}
public boolean isIsOfficialSurname() {
return isOfficialSurname;
}
public void setIsOfficialSurname(boolean isOfficialSurname) {
this.isOfficialSurname = isOfficialSurname;
}
public boolean isIsGotraInRegRequest() {
return isGotraInRegRequest;
}
public void setIsGotraInRegRequest(boolean isGotraInRegRequest) {
this.isGotraInRegRequest = isGotraInRegRequest;
}
public String getSubCasteLabel() {
return subCasteLabel;
}
public void setSubCasteLabel(String subCasteLabel) {
this.subCasteLabel = subCasteLabel;
}
public int getMissCallWaitSeconds() {
return missCallWaitSeconds;
}
public void setMissCallWaitSeconds(int missCallWaitSeconds) {
this.missCallWaitSeconds = missCallWaitSeconds;
}
public boolean isIsFBAccountKit() {
return isFBAccountKit;
}
public void setIsFBAccountKit(boolean isFBAccountKit) {
this.isFBAccountKit = isFBAccountKit;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getNavigation() {
return navigation;
}
public void setNavigation(String navigation) {
this.navigation = navigation;
}
}
答案 1 :(得分:0)
我认为有两种解决方案:
null
而不是整数。这是最简单的解决方案,您不需要做任何代码。此外,此解决方案将修复所有用户的崩溃。为该类编写自定义解析器。当您使用Gson
解析给定的类时,您可以编写一个自定义TypeAdapter
,指示Gson
如何解析您的Json
。我的课程实现如下:
public static class ResponseAdapter extends TypeAdapter<GetUIConfigResponse> {
@Override
public void write(JsonWriter jsonWriter, GetUIConfigResponse response) throws IOException {
// In case that you need to serialize the class again
// take a look at http://www.javacreed.com/gson-typeadapter-example/
}
@Override
public GetUIConfigResponse read(JsonReader jsonReader) throws IOException {
GetUIConfigResponse response = new GetUIConfigResponse();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
switch (jsonReader.nextName()) {
case "IsMissCallLogin":
response.setIsMissCallLogin(jsonReader.nextBoolean());
break;
case "IsOfficialSurname":
response.setIsOfficialSurname(jsonReader.nextBoolean());
break;
case "IsGotraInRegRequest":
response.setIsGotraInRegRequest(jsonReader.nextBoolean());
break;
case "SubCasteLabel":
response.setSubCasteLabel(jsonReader.nextString());
break;
case "MissCallWaitSeconds":
String waitString = jsonReader.nextString();
try {
int number = Integer.parseInt(waitString);
response.setMissCallWaitSeconds(number);
} catch (NumberFormatException ex) {
// in case of string set the field to a default value
response.setMissCallWaitSeconds(5);
}
break;
case "IsFBAccountKit":
response.setIsFBAccountKit(jsonReader.nextBoolean());
break;
case "Status":
String statusString = jsonReader.nextString();
try {
int number = Integer.parseInt(statusString);
response.setStatus(number);
} catch (NumberFormatException ex) {
// in case of string set the field to a default value
response.setStatus(0);
}
break;
case "Message":
response.setMessage(jsonReader.nextString());
break;
case "Navigation":
response.setNavigation(jsonReader.nextString());
break;
default:
jsonReader.skipValue();
}
}
jsonReader.endObject();
return response;
}
}
然后我像这样解析了Json(我从本地文件读取Json,因此变量inputStreamReader
):
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(GetUIConfigResponse.class, new ResponseAdapter());
Gson gson = gsonBuilder.create();
GetUIConfigResponse model = gson.fromJson(inputStreamReader, GetUIConfigResponse.class);
使用此代码,应该正确解析所有内容,如果整数字段具有字符串值,则将其转换为int(如果可能)。如果无法转换它,那么我设置一些在类的构造函数中找到的默认值。此解决方案的一个优点是它可以防止此类未来的情况(仅适用于这些字段),但如果您的应用程序已经启动,那么您可能会再次发现此次崩溃对于尚未更新应用程序的用户。< / p>
答案 2 :(得分:0)
Gson gson = new GsonBuilder().registerTypeAdapter(Long.class, new LongTypeAdapter()).create();
像
一样使用它{{1}}