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

无法使用 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"}]}



慕田峪9158850
浏览 177回答 1
1回答

UYOU

理想的情况是,如果您可以将响应更改为以下内容:[{&nbsp; &nbsp; &nbsp; &nbsp; "uid": "111",&nbsp; &nbsp; &nbsp; &nbsp; "name": "kiran"&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; "uid": "112",&nbsp; &nbsp; &nbsp; &nbsp; "name": "kumar"&nbsp; &nbsp; }]如果您无法更改它,请尝试以下操作: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(),&nbsp;typeFactory.constructCollectionType(List.class, PaymentResponse.class));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java