我正在使用 spring android 框架通过 Http GET 检索 json 数据。我得到以下相同的异常:
- 无法读取 JSON:无法从 START_OBJECT 令牌中反序列化 com.springandroidjsondemo.beans.LoginBean[] 的实例
bean (LoginBean) 正在关注
package com.springandroidjsondemo.beans;
public class LoginBean {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
安卓代码如下:
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson message converters
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
// Make the HTTP GET request, marshaling the response from JSON to an array of Events
ResponseEntity<LoginBean[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity,LoginBean[].class); // getting exception here
LoginBean[] loginBean = responseEntity.getBody();
String status = loginBean[0].getStatus();
来自服务器的 json 响应如下:
{"emp-data":[{"status":"true"}]}
我不确定 Jackson Marshalling 是否需要任何注释
请提出解决方案
谢谢!