将ZonedDateTime类型转换为Gson [英] Converting ZonedDateTime type to Gson

查看:389
本文介绍了将ZonedDateTime类型转换为Gson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有休息服务,返回对象的arraylist,我已经实现了jersy宁静的客户端来执行它,但我在转换ZonedDateTime类型为json的问题,所以我得到这个错误

 引发:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但是在第1行的列STRING 231路径$ [0] .lastmodifieddate 

我如何解决这个问题?

lastmodifieddate column in entity

  @Column(name =lastmodifieddate)
private ZonedDateTime lastmodifieddate;

// getter and setter

休息服务

  @RequestMapping(value =/ getScoreFactor,
method = RequestMethod.GET,
produce = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List< Scorefactor> getScoreFactor()抛出JSONException {
return scoreService.getScoreFactor();



jersy宁静客户


  try {

Client client = Client.create();
WebResource webResource =客户端
.resource(http:// localhost:8080 / adap / api / getScoreFactor);
ClientResponse response = webResource.accept(application / json)
.get(ClientResponse.class);

字符串输出= response.getEntity(String.class);

System.out.println(output - + output);
Type listType = new TypeToken< List< Scorefactor>>(){} .getType();

列表< Scorefactor> scorefactors = new Gson()。fromJson(output,listType);

System.out.println(scorefactors);

} catch(Exception e){

e.printStackTrace();



解决方案

修正它,这是更新后的代码

 客户端客户端= Client.create(); 
WebResource webResource =客户端
.resource(http:// localhost:8080 / adap / api / getScoreFactor);
ClientResponse response = webResource.accept(application / json)
.get(ClientResponse.class);

字符串输出= response.getEntity(String.class);

Gson gson = new GsonBuilder()。registerTypeAdapter(ZonedDateTime.class,new JsonDeserializer< ZonedDateTime>(){
@Override
public ZonedDateTime deserialize(JsonElement json,Type type, JsonDeserializationContext jsonDeserializationContext)throws JsonParseException {
return ZonedDateTime.parse(json.getAsJsonPrimitive()。getAsString());
}
})。create();

type listType = new TypeToken< List< Scorefactor>>(){} .getType();

列表< Scorefactor> scorefactors = gson.fromJson(output,listType);


I have rest service that return arraylist of object,and I have implemented jersy restful client to execute it,but I have problem in converting ZonedDateTime type to json so I get this error

 Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 231 path $[0].lastmodifieddate

How can i fix this problem?

lastmodifieddate column in entity

 @Column(name = "lastmodifieddate")
 private ZonedDateTime lastmodifieddate;

 //getter and setter

rest service

@RequestMapping(value = "/getScoreFactor",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<Scorefactor> getScoreFactor() throws JSONException {
    return scoreService.getScoreFactor();
}   

jersy restful client

  try {

        Client client = Client.create();
        WebResource webResource = client
           .resource("http://localhost:8080/adap/api/getScoreFactor");
        ClientResponse response = webResource.accept("application/json")
                   .get(ClientResponse.class);

        String output =  response.getEntity(String.class);

        System.out.println("output--"+output);
        Type listType =  new TypeToken<List<Scorefactor>>() {}.getType();

        List<Scorefactor> scorefactors = new Gson().fromJson(output,listType);

        System.out.println(scorefactors);

    } catch (Exception e) {

        e.printStackTrace();

}    

解决方案

I have fixed it,This is the code after updates

Client client = Client.create();
        WebResource webResource = client
           .resource("http://localhost:8080/adap/api/getScoreFactor");
        ClientResponse response = webResource.accept("application/json")
                   .get(ClientResponse.class);

        String output =  response.getEntity(String.class);

        Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new JsonDeserializer<ZonedDateTime>() {
            @Override
            public ZonedDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                return ZonedDateTime.parse(json.getAsJsonPrimitive().getAsString());
            }
            }).create();

        Type listType =  new TypeToken<List<Scorefactor>>() {}.getType();

        List<Scorefactor> scorefactors = gson.fromJson(output,listType);

这篇关于将ZonedDateTime类型转换为Gson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆