在我的java项目中有一个gson反序列化函数,它会导致以下错误。
java.lang.NumberFormatException: For input string: "2017-01-28 13:28:20"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.parseLong(Long.java:631)
at com.google.gson.JsonPrimitive.getAsLong(JsonPrimitive.java:238)
at com.example.myproject.controller.MyController$1.deserialize(MyController.java:222)
at com.example.myproject.controller.MyController$1.deserialize(MyController.java:1)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.internal.bind.TypeAdapters$22$1.read(TypeAdapters.java:526)
at com.google.gson.internal.bind.TypeAdapters$22$1.read(TypeAdapters.java:524)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
当我反序列化下面的json字符串时,我收到此错误。
jsonString = {"repList":[{"createdOn":"2017-01-28 13:28:20","date":"2016-11-17 00:00:00","description":"","id":45,"userId":10}],"subList":[{"attachmentCount":0,"dateOn":"2017-01-28 13:28:20","id":86,"screenId":1,"sync":"1","repId":45,"userId":10}]}
下面是我完成gson反序列化的函数。
SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ENGLISH);
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext deserializationContext) throws JsonParseException {
String frStr = dtf.format(new Date(json.getAsJsonPrimitive().getAsLong()));
dtf.setTimeZone(TimeZone.getDefault());
Date retDate =null;
try {
retDate = dtf.parse(frStr);
} catch (ParseException e) {
e.printStackTrace();
}
return retDate;
}
});
gson = builder.create();
MainAccounts mainAcc = gson.fromJson(jsonString, MainAccounts.class);
我注意到的是当我将json原语转换为long以下时出现异常
json.getAsJsonPrimitive().getAsLong()
答案 0 :(得分:4)
您需要直接获得JsonElement
的真实价值:通过调用方法String
来long
而不是getAsString()
,否则您将获得{ {1}}正如您已经注意到的那样,所以NumberFormatException
应该是:
JsonDeserializer
NB:实际上,上面提到的类型为new JsonDeserializer<Date>() {
private SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
{
// Indicate the time zone of the input date
dtf.setTimeZone(TimeZone.getTimeZone("Asia/Dubai"));
}
@Override
public Date deserialize(JsonElement json, Type type,
JsonDeserializationContext deserializationContext) throws JsonParseException {
try {
// Get the json element as a String and parse it to get a Date
return dtf.parse(json.getAsString());
} catch (ParseException e) {
// Throw a JsonParseException in case of a parsing error
throw new JsonParseException(e);
}
}
}
的匿名内部类不是线程安全的,因为JsonDeserializer
它不是线程安全的,所以避免共享它的实例,在您想要反序列化JSON内容的任何时候创建一个新实例。
答案 1 :(得分:2)
我认为问题是该值不是表示long的String,因此您无法解析它。您应该使用String frStr = dtf.parse(json.getAsJsonPrimitivie().getAsString())