2012-07-03 66 views

回答

5

您需要創建日期自定義解串器。例如創建這個類:

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

然後一個JSON字符串轉換成POJO前:

GsonBuilder builder = new GsonBuilder(); 
builder.registerTypeAdapter(Date.class, new DotNetDateDeserializer()); 
Gson gson = builder.create(); 
gson.fromJson(jsonString, Class); 
+0

我以Java'Date.class'爲例,但您可以用'Calendar.class'替換它。 – azgolfer

1

使用UNIX時間戳,而不是日期時間,日期時間是模糊的結構

+0

我怎麼會得到從UNIX時間戳的日曆?這是內置的還是我將不得不編寫一個自定義的反序列化器? – Dunken

+0

之前在日曆中使用轉換爲日期時間,但在運輸使用數字它是不可改變的 –