1

DateTime という名前のカスタム オブジェクトがあります。メソッド registerTypeAdapter で TypeAdapter を登録します。GSON はこのアダプタを無視していると思います。GSON jsonオブジェクトで日付を検出する方法は? アダプターと json の文字列の形式との間のリンクを確立する必要がありますか?

   builder.registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() 
          {

            public DateTime deserialize(JsonElement arg0, Type arg1,JsonDeserializationContext arg2) throws JsonParseException 
            {
                try{return new DateTime(arg0.getAsString());}
                catch (Exception e){
                    return null;
                    }
            }
});
4

1 に答える 1

0

Json は名前と値のペアで提供されます。ほぼ確実に例外が発生し、それを食べています。名前と値のペアの値を取り出して、新しい DateTime に渡す必要があります。JSON が { "date": 213424234 } のようなものである場合、次のようにする必要があります。

DateTime date = new DateTime(jsonElement.getAsJsonObject().get( "date" ).getAsLong());
于 2013-07-25T22:48:56.530 に答える