将ZonedDateTime类型转换为Gson [英] Converting ZonedDateTime type to Gson
本文介绍了将ZonedDateTime类型转换为Gson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
引发: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屋!
查看全文