无法从START_OBJECT令牌中反序列化PaymentResponse []实例

时间:2018-10-01 05:11:29

标签: java json jackson jackson-databind

无法使用杰克逊对象映射器读取响应。摆脱START_OBJECT令牌。这是输入的json和错误消息。 PaymentResponse java pojo是通用代码,我无权添加任何注释。添加反序列化配置有帮助吗?

ObjectMapper objectMapper = new ObjectMapper();
PaymentResponse[] paymentResponse  = objectMapper.readValue(serverResponseStr, PaymentResponse[].class);

{
    "PaymentResponse[]": [
        {
            "uid": "111",
            "name": "kiran"
        },
        {
            "uid": "112",
            "name": "kumar"
        }
    ]
}

Can not deserialize instance of PaymentResponse[] out of START_OBJECT token

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of PaymentResponse[] out of START_OBJECT token
 at [Source: {"PaymentResponse[]": [{"uid": "111","name": "kiran"},{"uid": "112","name": "kumar"}]}

1 个答案:

答案 0 :(得分:0)

理想的情况是,您可以将响应更改为以下内容:

[{
        "uid": "111",
        "name": "kiran"
    }, {
        "uid": "112",
        "name": "kumar"
    }
]

如果无法更改,请尝试以下操作:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
JsonNode array = objectMapper.readValue(anotherResponseStr, JsonNode.class);
JsonNode paymentResponse = array.get("PaymentResponse[]");

List<PaymentResponse> p = objectMapper.readValue(paymentResponse.toString(), 
typeFactory.constructCollectionType(List.class, PaymentResponse.class));