为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕田峪9158850 2021-10-20 15:17:59
无法使用 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 tokencom.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 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

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


[{

        "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));


查看完整回答
反对 回复 2021-10-20
  • 1 回答
  • 0 关注
  • 176 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信