我試圖反序列化一個DateTime(C#)作爲JSON(StartDate =/Date(1341348517698 + 0200)/) ...gson:轉換/日期(1341348517698 + 0200)/日曆
我已經嘗試沒有成功如下:
ObjectWithCalender cal = gson.fromJson(jsonWithDate, ObjectWithCalender.class);
我試圖反序列化一個DateTime(C#)作爲JSON(StartDate =/Date(1341348517698 + 0200)/) ...gson:轉換/日期(1341348517698 + 0200)/日曆
我已經嘗試沒有成功如下:
ObjectWithCalender cal = gson.fromJson(jsonWithDate, ObjectWithCalender.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
}
}
然後一個JSON字符串轉換成POJO前:
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DotNetDateDeserializer());
Gson gson = builder.create();
gson.fromJson(jsonString, Class);
使用UNIX時間戳,而不是日期時間,日期時間是模糊的結構
我怎麼會得到從UNIX時間戳的日曆?這是內置的還是我將不得不編寫一個自定義的反序列化器? – Dunken
之前在日曆中使用轉換爲日期時間,但在運輸使用數字它是不可改變的 –
我以Java'Date.class'爲例,但您可以用'Calendar.class'替換它。 – azgolfer