无法将JSON映射到Java bean

前端之家收集整理的这篇文章主要介绍了无法将JSON映射到Java bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我收到错误

线程“main”中的异常org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法从START_ARRAY令牌中反序列化com.zee.FilterVO的实例.在[来源:sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1b54362; line:1,column:1];嵌套异常是org.codehaus.jackson.map.JsonMappingException ::无法从START_ARRAY令牌中反序列化com.zee.FilterVO的实例

Java调用获取JSON:

ResponseEntity

用于映射JSON的Java Bean:

public class FilterVO {

    private String userName;
    private String expirationTime;
    private String lastMatchedTime;
    private String state;
    private Integer matchedTodayCount;
    private Boolean useRegEx;
    private List

JSON我想要得到的:

[{
    "userName": "Z111111","expirationTime": "2015-03-15T14:00:00.000-0400","activationTime": "1969-12-31T19:00:00.000-0500","hosts": ["aa","bb"],"matchStrings": [" aa is not running"," bb is not running"],"useRegEx": false,"nameValuePairs": {
        "KEY VALUE": "asasa","PROGRAM": "dsds","REGION": "dfsffs"
    },"objectId": "SOMEID:20141015140803579-802","matchedTodayCount": 190,"lastMatchedTime": "2015-03-09T07:53:20.366-0400","state": "ACTIVE","description": "Blah Blah Blah..."
}]

Initially I thought I am facing this issue because of deserialization of one element Array. So I modified my java code.

private void getFilter() {
        RestTemplate restTemplate = getCustomRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.set("access-token",getAccessToken());
        HttpEntity

我仍然得到同样的错误.

我在我的类路径中有以下杰克逊罐子:

jackson-annotations-2.5.1.jar
jackson-core-2.5.1.jar
jackson-core-asl-1.9.13.jar
jackson-databind-2.5.1.jar
jackson-datatype-joda-2.5.1.jar
jackson-mapper-asl-1.9.13.jar

完整堆栈跟踪:

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@bc5596; line: 1,column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@bc5596; line: 1,column: 1]
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:179)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:171)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:693)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:679)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:496)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:465)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:416)
    at com.fidelity.selfservice.test.JMagicRestAPI.getFilter(JMagicRestAPI.java:63)
    at com.fidelity.selfservice.test.JMagicRestAPI.main(JMagicRestAPI.java:46)
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.zee.FilterVO out of START_ARRAY token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@bc5596; line: 1,column: 1]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    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.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:875)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
    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.readJavaType(MappingJacksonHttpMessageConverter.java:176)
    ... 9 more
最佳答案
问题是你正在获得一个JSON数组,但是你正试图用你的情况下的POJO,FilterVO反序列化那个JSON.

尝试更改此行:

ResponseEntity

通过这个:

ResponseEntity

现在你将有一个List< FilterVO>跟…共事.

猜你在找的Spring相关文章