2012-07-10 50 views
1

我有一個映射問題,它使用jackson庫將Json API回調轉換爲自定義對象。Json DateTime對象到GregorianCalendar

我有我的用戶自定義類:

public class User { 
    protected String id; 
    protected GregorianCalendar registredAt; 
    protected String username; 
    protected Team team; 

    public User() { 

    } 

    public User(String id, GregorianCalendar registredAt, String username, Team team) { 
     this.id = id; 
     this.registredAt = registredAt; 
     this.username = username; 
     this.team = team; 
    } 

    public User(HashMap<String, Object> attributes) { 
     this.id = (String) attributes.get("id"); 
     this.registredAt = (GregorianCalendar) attributes.get("registredAt"); 
     this.username = (String) attributes.get("username"); 
     this.team = (Team) attributes.get("team"); 
    } 
} 

這裏是我的回調:

{ 
    id: "4feab37c5600bbd56f000132", 
    registredAt: { 
     date: "2012-06-27 09:17:15", 
     timezone_type: 3, 
     timezone: "Europe/Paris" 
    }, 
    username: "John Doe", 
    team: { 
     id: "4feab37c5600bbd56f000130", 
     indice: 1, 
     name: "John Doe's Team", 
     score: 200 
    } 
} 

然後,我嘗試初始化一個用戶對象:

// Code from a doInBackground() method of a custom AsyncTask. 
URL completePath = new URL(apiBaseURL + "api/user/4feab37c5600bbd56f000132/show"); 
APIconnection = (HttpURLConnection) completePath.openConnection(); 
APIconnection.setDoInput(true); 
APIconnection.connect(); 
callbackParser = jsonFactory.createJsonParser(APIconnection.getInputStream()); 
response = objectMapper.readValue(callbackParser, User.class); 
// Catch a JsonMappingException : 
Can not deserialize instance of java.util.Calendar out of START_OBJECT token at [Source: org.apache.har[email protected]4082c7a0; line: 1, column: 33] (through reference chain: classes.models.User["registredAt"]) 

我想問題是回調registredAt對象是一個json_encoded PHP \ DateTime實例... 應該我更改註冊的Java用戶對象的類型? 或者我的構造函數中缺少一些東西?

+0

貴公曆類從日曆擴展?它是否實現了Serializable接口? – 2012-07-10 11:34:38

+0

這是一個擴展java.util.Calendar中,其中實現Serializable inteface :) 的問題是一樣的,當我更換的GregorianCalendar由Calendar類(java.util中的命名空間) 不能反序列化java.util.GregorianCalendar中的類java.util.Calendar的實例超出了START_OBJECT標記。 – 2012-07-10 12:12:48

+0

可串行化是無關緊要的 - 它不會被JSON序列化器使用,因爲它只意味着JDK可以生成二進制表示。 – StaxMan 2012-07-10 19:46:25

回答

1

最後,我做出這樣的事情來解決我的問題:

public User(HashMap<String, Object> attributes) { 
    this.id = (String) attributes.get("id"); 
    HashMap <String, Object> registredAt = (HashMap<String, Object>) attributes.get("registredAt"); 
    IMDateFormatter formatter = new IMDateFormatter(); 
    this.registredAt = formatter.getCalendarFromJson((String) registredAt.get("date")); 
    [...] 
} 

而且IMDateFormatter格式化我的日期,以簡單的方式:

public class IMDateFormatter extends DateFormat { 
    public IMDateFormatter() { 
    } 

    @Override 
    public StringBuffer format(Date arg0, StringBuffer arg1, FieldPosition arg2) { 

     return null; 
    } 

    @Override 
    public Date parse(String dateFromJson, ParsePosition arg1) { 

     return null; 
    } 

    public GregorianCalendar getCalendarFromJson(String dateFromJson) { 
     GregorianCalendar calendarFromJson = null; 

     try { 
      GregorianCalendar tmpCalendar = new GregorianCalendar(); 
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.FRANCE); 
      tmpCalendar.setTime(df.parse(dateFromJson)); 
      calendarFromJson = tmpCalendar; 
     } 
     catch (NullPointerException NPE) { 
      NPE.printStackTrace(); 
     } 
     catch (ParseException PE) { 
      PE.printStackTrace(); 
     } 

     return calendarFromJson; 
    } 
} 
1

您應該使用日期和日曆的標準ISO-8601字符串表示法;或者,如果您要使用JSON對象,請爲GregorianCalendar類型編寫您自己的JsonDeserializer

+0

我不關心時區,我只是想和iOS上的AFNetworking一樣簡單... 我應該更改API來導出日期而不是日期時間對象嗎? – 2012-07-11 13:58:51

+1

這可能是最簡單的方法。 – StaxMan 2012-07-13 18:38:35