2014-04-03 26 views
9

Używam RestTemplete dostać danych JSON z API odpoczynek i używam Gson do analizowania danych z formatu json do obiektówcom.google.gson.JsonSyntaxException podczas próby analizowania Date/Time w 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)); 

ale mam ten problem z datą, co mam zrobić ..

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

mój Pojo że zawiera inne POJOs w jego organizmie

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

to ths json zwrócony przez mojego API

[ 
    { 
     "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

Jak wygląda Twój json? Jak wygląda twój pojo? –

+0

Próbujesz rzucić długą historię. – rpax

+0

Te wartości, "1384732800000", wydają się znacznikami czasowymi. Gson nie jest skonfigurowany do analizowania dat za pomocą znaczników czasu. Będziesz musiał skonfigurować go za pomocą niestandardowego 'TypeAdapter'. –

Odpowiedz

4

Co W końcu poszedłem do mojego projektu API i utworzyłem 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); 
    } 
} 
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); 
    } 
} 

że powrót formacie yyyy-MM-DD i adnotacjami pola bieżąco z

@JsonSerialize(using = CustomDateSerializer.class) 

w mojej aplikacji Android stworzyłem obiekt Gson jak

  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(); 

i działa do teraz .. dzięki za pomoc doceniam to

+0

Gdzie napisać tę @JsonSerialize (używając = CustomDateSerializer.class) – KJEjava48

+1

Nie jest już konieczne tworzenie Custom Serializer.Zobacz tutaj na przykład: http://stackoverflow.com/a/34187419/1103584 – DiscDev

0

Wartość 1382828400000 jest długi (czas w milisekundach). Mówisz GSON, że pole to jest Date i nie może automatycznie przekonwertować long na Date.

Musisz podać swoje pola w długich wartościach

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

i po GSON rzuca ciąg JSON do żądanej Appel klasy przykład skonstruować inny obiekt z tych dziedzin jak daty i konwertować je podczas przypisywania wartości do nowy obiekt.

Inną alternatywą jest wdrożenie własnego klienta Deserializator:

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(); 
    } 
} 

Musisz ustawić niestandardową Deserializator na żądanych polach, na temat metod setter, jak:

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

niestandardowe Serializers nie są już konieczne - wystarczy skorzystać GsonBuilder i określ format daty, takie jak:

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); 
Powiązane problemy