gpt4 book ai didi

java - com.google.gson.JsonSyntaxException 试图解析 json 中的日期/时间

转载 作者:搜寻专家 更新时间:2023-10-30 19:58:25 25 4
gpt4 key购买 nike

我正在使用 RestTemplete 从 rest api 获取 json 数据,我正在使用 Gson 将数据从 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));

但是我遇到了 Date 的问题,我该怎么办..

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"
}
},
.....
]

最佳答案

不再需要自定义序列化程序 - 只需使用 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);

关于java - com.google.gson.JsonSyntaxException 试图解析 json 中的日期/时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22841306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com