2017-01-28 8 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); 

は私がこの問題を解決することができますデシリアライズのmethod.How内部

json.getAsJsonPrimitive().getAsLong() 

以下のように長いとJSONプリミティブ変換するときに例外が来るのですか?私を助けてください。

答えて

4

を使用する必要がありますむしろである:

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); 
     } 
    } 
} 

NB:そのまま、タイプJsonDeserializerの匿名の内部クラスは、上記提案されたので、それのインスタンスを共有避けるスレッドセーフitslefないという理由SimpleDateFormatスレッドセーフではありません、 c JSONコンテンツを逆シリアル化する必要があるときはいつでも、新しいインスタンスを再作成してください。

+0

ok.its now.Alsoには日付の値が入ります私はフォーマットと一緒にこれを行うことができますどのように私は、インドのtime.Howに日付の値が変更されるようにサーバータイムゾーン(インドにある)に変換する必要がありますか? – 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ではないと仮定して、そのように解析することはできません。あなたはすでに気づいているので、あなたのJsonDeserializerが必要としてそうでなければ、NumberFormatExceptionを取得しますStringないlong方法getAsString()呼び出すことにより:あなたはおそらく、あなたはそれが本当にあるとして直接あなたのJsonElementの値を取得する必要がありString frStr = dtf.parse(json.getAsJsonPrimitivie().getAsString())

+0

json.getAsJsonPrimitive()の値を表示すると、これは "2017-01-28 13:28:20"となります。 – KJEjava48

関連する問題