当前位置: 动力学知识库 > 问答 > 编程问答 >

android - gson: convert /Date(1341348517698+0200)/ to Calendar

问题描述:

I'm trying to deserialize a DateTime (C#) which comes as JSON (StartDate=/Date(1341348517698+0200)/) to an Android Calendar object using gson...

I already tried the following without success:

ObjectWithCalender cal = gson.fromJson(jsonWithDate, ObjectWithCalender.class);

网友答案:

You need to create a custom deserializer for Date. e.g. create this class:

public class DotNetDateDeserializer implements JsonDeserializer<Date> {
   @Override
   public Date deserialize(JsonElement json, Type typfOfT, JsonDeserializationContext context) {
      // this is the place where you convert .NET timestamp into Java Date object
   } 
}

Then before converting a JSON string into POJO:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DotNetDateDeserializer());
Gson gson = builder.create();
gson.fromJson(jsonString, Class);
网友答案:

use unix timestamp instead of datetime, datetime is ambiguous struct

分享给朋友:
您可能感兴趣的文章:
随机阅读: