java – 尝试在json中解析日期/时间时出现com.google.gson.JsonSyntaxException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 尝试在json中解析日期/时间时出现com.google.gson.JsonSyntaxException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2108字,纯文字阅读大概需要4分钟。
内容图文
我正在使用RestTemplete从rest api获取json数据,而我正在使用Gson将数据从json格式解析为Object
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包含其他pojos的身体
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 – 尝试在json中解析日期/时间时出现com.google.gson.JsonSyntaxException全部内容,希望文章能够帮你解决java – 尝试在json中解析日期/时间时出现com.google.gson.JsonSyntaxException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。