Gson: JsonSyntaxException on date

后端 未结 4 664
别那么骄傲
别那么骄傲 2020-12-10 13:08

I am trying to use Gson to deserialize a json array, but am currently getting a JsonSyntaxException. The json string was created by a .NET MVC3 web service using JsonResult

4条回答
  •  时光说笑
    2020-12-10 13:28

    Serialize and Deserialize methoda. Register this as a Adapter for GSON

    JsonSerializer ser = new JsonSerializer() {
    @Override
    public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext 
             context) {
    return src == null ? null : new JsonPrimitive(src.getTime());
    }
    };
    
    JsonDeserializer deser = new JsonDeserializer() {
     @Override
     public Date deserialize(JsonElement json, Type typeOfT,
       JsonDeserializationContext context) throws JsonParseException {
    return json == null ? null : new Date(json.getAsLong());
     }
     };
    
    Gson gson = new GsonBuilder()
    .registerTypeAdapter(Date.class, ser)
    .registerTypeAdapter(Date.class, deser).create();
    

提交回复
热议问题