解组 Json 列表中的 Json 数组 [英] Unmarshal a Json Array in a Json List
问题描述
我需要获得所有电影的列表.
I need to get the list of all the films.
我处于这种情况,我不知道如何管理它.我的项目分为两个较小的项目.后端项目和前端项目.生成包含电影列表的 Json 的后端部分.
i am in this situation and i dont know how to manage it. My project is divided in two smaller project. Back-end project and front-end project. Back-end part that produce a Json that contains a list of films.
服务有这种模式
@GET
@Produce(json) // here is a particular library and it funcion correctly.
List<Film> getAllFilms
调用此服务的输出具有以下模式:
The output calling this service has this pattern:
[{"title:abc","time": 5486448}, {....}, {....}]
在前端项目中,我使用的是 Resteasy .我创建了一个类服务来调用后端并管理响应
At the Front-end project i am using Resteasy . I have create a class service to call the back-end and to manage the response
List<Film> film= new ArrayList<>();
try{
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost:8080/film");
Response response = target.request().get();
film= List<Film>) response.readEntity(Film.class);
我有这种类型的例外:
javax.ws.rs.ProcessingException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of FILM out of START_ARRAY token
现在我正在尝试理解一些东西,但有很多材料,我正在四处走动.如何将数组解组为列表?
Now i am trying to understand something but there is full of material and i am loosing around. How can i unmarshall an array to a list ?
推荐答案
可以使用jackson的readValue
方法将其转换为List
You can use readValue
method of jackson to convert it to List
public List<Film> convert(String jsonString) throws JsonParseException, JsonMappingException,
IOException {
ObjectMapper objectMapper = new ObjectMapper();
List<Film> filmList = objectMapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructCollectionType(
List.class, Film.class));
return filmList;
}
这篇关于解组 Json 列表中的 Json 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!