2017-01-28 83 views
1

在我的java項目中有一個gson反序列化函數會導致以下錯誤。java.lang.NumberFormatException:對於輸入字符串:「2017-01-28 13:28:20」

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原語,只要下面

json.getAsJsonPrimitive().getAsLong() 

的反序列化method.How我能解決這個裏面呢?請大家幫幫我。

回答

4

您需要直接讓你JsonElement的價值,因爲它確實就是:String不是long通過調用方法getAsString()否則你將得到一個NumberFormatException因爲你已經注意到了,所以你JsonDeserializer應寧爲:

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類型的上述建議是不是線程安全的,因爲SimpleDateFormat是itslef不是線程安全的,從而避免共享它的一個實例, C任何時候想要反序列化JSON內容都可以創建一個新實例。

+0

ok.its正在工作。上面的json字符串帶有日期值對應於迪拜時區,現在我需要將其轉換爲服務器時區(即在印度),以便日期值更改爲印度時間。如何隨格式一起執行此操作? – KJEjava48

+0

@ KJEjava48解析時最重要的是輸入時區,因爲java.util.Date是時區無關的(它實際上是UTC中的時間戳),請檢查[this](http://stackoverflow.com/questions/) 1516213/is-java-util-date-using-timezone)瞭解更多詳細信息。另請參閱http://stackoverflow.com/questions/7670355/convert-date-time-for-given-timezone-java –

+1

AFAIR,'SimpleDateFormat'不是線程安全的。 –

1

我認爲問題在於該值不是一個表示長的字符串,因此您不能像這樣解析它。你或許應該使用String frStr = dtf.parse(json.getAsJsonPrimitivie().getAsString())

+0

當我打印json.getAsJsonPrimitive()的值這是我得到的值「2017-01-28 13:28:20」 – KJEjava48

相關問題