2014-04-03 82 views
9

我使用RestTemplete從一個REST API獲取JSON數據,我使用GSON解析來自JSON格式的數據對象com.google.gson.JsonSyntaxException當試圖解析日期/時間JSON

Gson gson = new Gson(); 

restTemplate = new RestTemplate(); 
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter()); 
restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); 

List<Appel> resultList = null; 

resultList = Arrays.asList(restTemplate.getForObject(urlService, Appel[].class)); 

,但我得到了這個問題的日期,我應該怎麼辦..

Could not read JSON: 1382828400000; nested exception is com.google.gson.JsonSyntaxException: 1382828400000 

我的POJO包含在它的身上其他的POJO

public class Appel implements Serializable { 

    private Integer numOrdre; 
    private String reference; 
    private String objet; 
    private String organisme; 
    private Double budget; 
    private Double caution; 
    private Date dateParution; 
    private Date heureParution; 
    private Date dateLimite; 
    private Date heureLimite; 
    private List<Support> supportList; 
    private Ville villeid; 
    private Categorie categorieid; 

    public Appel() { 
    } 

    public Appel(Integer numOrdre, String reference, String objet, String organisme, Date dateParution, Date heureParution, Date dateLimite) { 
     this.numOrdre = numOrdre; 
     this.reference = reference; 
     this.objet = objet; 
     this.organisme = organisme; 
     this.dateParution = dateParution; 
     this.heureParution = heureParution; 
     this.dateLimite = dateLimite; 
    } 

這是我的API返回的部份JSON

[ 
    { 
     "numOrdre": 918272, 
     "reference": "some text", 
     "objet": "some text", 
     "organisme": "some text", 
     "budget": 3000000, 
     "caution": 3000000, 
     "dateParution": 1382828400000, 
     "heureParution": 59400000, 
     "dateLimite": 1389657600000, 
     "heureLimite": 34200000, 
     "supportList": 
     [ 
      { 
       "id": 1, 
       "nom": "some text", 
       "dateSupport": 1384732800000, 
       "pgCol": "013/01" 
      }, 
      { 
       "id": 2, 
       "nom": "some text", 
       "dateSupport": 1380236400000, 
       "pgCol": "011/01" 
      } 
     ], 
     "villeid": 
     { 
      "id": 2, 
      "nom": "Ville", 
      "paysid": 
      { 
       "id": 1, 
       "nom": "Pays" 
      } 
     }, 
     "categorieid": 
     { 
      "id": 1, 
      "description": "some text" 
     } 
    }, 
    ..... 
] 
+0

你的json是什麼樣的?你的pojo是什麼樣的? –

+0

您正在嘗試投入很長時間。 – rpax

+0

這些值,'1384732800000',看起來像時間戳。 Gson未設置爲使用時間戳解析日期。您必須使用自定義的「TypeAdapter」對其進行配置。 –

回答

4

什麼我終於做了我的API項目並創建一個CustomSerializer

public class CustomDateSerializer extends JsonSerializer<Date> { 

    @Override 
    public void serialize(Date t, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { 
     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
     String formattedDate = formatter.format(t); 

     jg.writeString(formattedDate); 
    } 
} 

,在我的Android應用程序與

@JsonSerialize(using = CustomDateSerializer.class) 

返回格式YYYY-MM-DD,我批註日期字段我創建GSON對象

  Reader reader = new InputStreamReader(content); 

      GsonBuilder gsonBuilder = new GsonBuilder(); 
      gsonBuilder.setDateFormat("yyyy-MM-dd"); 
      Gson gson = gsonBuilder.create(); 
      appels = Arrays.asList(gson.fromJson(reader, Appel[].class)); 
      content.close(); 

和它的作品現在...謝謝你幫助,我很感激

+0

哪裏寫這@JsonSerialize(使用= CustomDateSerializer.class) – KJEjava48

+1

它不再需要創建一個自定義序列。看到這裏的例子:http://stackoverflow.com/a/34187419/1103584 – DiscDev

0

1382828400000值是一個長(以毫秒爲單位的時間)。您正在告訴GSON該字段爲Date,並且它不能自動將long轉換爲Date

你只要值

private long dateParution; 
private long heureParution; 
private long dateLimite; 
private long heureLimite; 

GSON注塑JSON字符串所需Appel類的實例,構建以這些領域爲日期另一個對象,並將其轉換而分配值到指定域新的對象。

另一種選擇是實現自己的自定義解串器:

public class CustomDateDeserializer extends DateDeserializer { 
    @Override 
    public Date deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException { 
     // get the value from the JSON 
     long timeInMilliseconds = Long.parseLong(jsonParser.getText()); 

     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(timeInMilliseconds); 
     return calendar.getTime(); 
    } 
} 

必須設置該自定義解串器在您需要的字段,在setter方法,如:

@JsonDeserialize(using=CustomDateDeserializer.class) 
public void setDateParution(Date dateParution) { 
    this.dateParution = dateParution; 
} 
4

定製序列不再是必需的 - 簡單地使用GsonBuilder,並指定日期格式,因爲這樣的:

Timestamp t = new Timestamp(System.currentTimeMillis()); 

String json = new GsonBuilder() 
       .setDateFormat("yyyy-MM-dd hh:mm:ss.S") 
       .create() 
       .toJson(t); 

System.out.println(json);