Tôi đang sử dụng RestTemplete để lấy dữ liệu json từ một api nghỉ ngơi và tôi đang sử dụng Gson để phân tích dữ liệu từ định dạng json vào Objectcom.google.gson.JsonSyntaxException khi cố gắng phân tích Date/Time trong 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));
nhưng tôi nhận được vấn đề này với ngày, tôi nên làm gì ..
Could not read JSON: 1382828400000; nested exception is com.google.gson.JsonSyntaxException: 1382828400000
POJO của tôi có chứa các POJO khác trong cơ thể của nó
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;
}
đây là ths json trả về bởi API của tôi
[
{
"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"
}
},
.....
]
Json của bạn trông như thế nào? Pojo của bạn trông như thế nào? –
Bạn đang cố gắng truyền một thời gian dài. – rpax
Các giá trị đó, '1384732800000', có vẻ như dấu thời gian. Gson không được thiết lập để phân tích cú pháp các ngày có dấu thời gian. Bạn sẽ phải cấu hình nó với một 'TypeAdapter' tùy chỉnh. –