无法读取JSON?从json映射到pojo时出现问题

时间:2015-10-22 17:17:37

标签: android jackson robospice

最近,我开始了解这个名为 Robospice 的库。在完成指南实施后,我开始使用它,但redistemplate无法将 Json 映射到 POJO

这是 POJO

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "results")
public class MovieData {
    private Long id;
    private String poster_path;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPoster_path() {
        return poster_path;
    }

    public void setPoster_path(String poster_path) {
        this.poster_path = poster_path;
    }
}

以下是上面的pojo

的列表类
public class MovieDataList extends ArrayList<MovieData> {
    private static final long serialVersionUID = 1L;
}

这是它尝试绑定到pojo的地方。

    @Override
    public MovieDataList loadDataFromNetwork() throws Exception {
        String url = String.format("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=%s",api_key);
        Log.v(LOG_TAG,url);
        return getRestTemplate().getForObject(url,MovieDataList.class);
    }

logcat的

10-22 22:44:08.430  25614-25635/com.singh.ritesh.movies E//DefaultRequestRunner.java:154﹕ 22:44:08.429 Thread-18129 An exception occurred during request network execution :Could not read JSON: Can not deserialize instance of com.singh.ritesh.movies.model.MovieDataList out of START_OBJECT token
            at [Source: buffer(com.android.okio.GzipSource@3bbd7348).inputStream(); line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.singh.ritesh.movies.model.MovieDataList out of START_OBJECT token
            at [Source: buffer(com.android.okio.GzipSource@3bbd7348).inputStream(); line: 1, column: 1]
    org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.singh.ritesh.movies.model.MovieDataList out of START_OBJECT token
            at [Source: buffer(com.android.okio.GzipSource@3bbd7348).inputStream(); line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.singh.ritesh.movies.model.MovieDataList out of START_OBJECT token
            at [Source: buffer(com.android.okio.GzipSource@3bbd7348).inputStream(); line: 1, column: 1]
            at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:125)
            at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
            at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
            at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
            at com.singh.ritesh.movies.request.MovieDataRequest.loadDataFromNetwork(MovieDataRequest.java:22)
            at com.singh.ritesh.movies.request.MovieDataRequest.loadDataFromNetwork(MovieDataRequest.java:11)
            at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:48)
            at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:150)
            at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:217)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.singh.ritesh.movies.model.MovieDataList out of START_OBJECT token
            at [Source: buffer(com.android.okio.GzipSource@3bbd7348).inputStream(); line: 1, column: 1]
            at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
            at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
            at org.codehaus.jackson.map.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:246)
            at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:204)
            at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:194)
            at org.codehaus.jackson.map.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
            at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
            at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
            at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:122)
            at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
            at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
            at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
            at com.singh.ritesh.movies.request.MovieDataRequest.loadDataFromNetwork(MovieDataRequest.java:22)
            at com.singh.ritesh.movies.request.MovieDataRequest.loadDataFromNetwork(MovieDataRequest.java:11)
            at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:48)
            at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:150)
            at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:217)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

来自服务器的响应

{
  "page": 1,
  "results": [
    {
      "adult": false,
      "backdrop_path": "/dkMD5qlogeRMiEixC4YNPUvax2T.jpg",
      "genre_ids": [
        28,
        12,
        878,
        53
      ],
      "id": 135397,
      "original_language": "en",
      "original_title": "Jurassic World",
      "overview": "Twenty-two years after the events of Jurassic Park, Isla Nublar now features a fully functioning dinosaur theme park, Jurassic World, as originally envisioned by John Hammond.",
      "release_date": "2015-06-12",
      "poster_path": "/uXZYawqUsChGSj54wcuBtEdUJbh.jpg",
      "popularity": 88.551849,
      "title": "Jurassic World",
      "video": false,
      "vote_average": 7.1,
      "vote_count": 435
    }
    ]
}

任何帮助将不胜感激。谢谢提前。

我终于解决了这个问题,服务器返回的数据是一个单独的jsonObject,我将它绑定到ArrayList。

我又创建了一个课程。

@JsonIgnoreProperties(ignoreUnknown = true)
public class MovieDataResult {
    private List<MovieData> results;

    public List<MovieData> getResults() {
        return results;
    }

    public void setResults(List<MovieData> results) {
        this.results = results;
    }
}

返回的jsonObject被绑定到上面的类。哪个内容绑定到movieData pojo。

@Override
    public MovieDataResult loadDataFromNetwork() throws Exception {
        String url = String.format("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=%s",api_key);
        return getRestTemplate().getForObject(url,MovieDataResult.class);
    }

0 个答案:

没有答案