当前位置: 首页 > 知识库问答 >
问题:

无法反序列化不符合START_OBJECT标记的PaymentResponse[]实例

汪甫
2023-03-14

无法使用jackson ObjectMapper读取响应。正在脱离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个答案

史烈
2023-03-14

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

[{
        "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));
 类似资料: