我有来自payfort的JSON来读取交易,试图将其解析为POJO,但总是给我不匹配错误
[
[
{
"response_code": "04000",
"card_holder_name": null,
"acquirer_mid": "***",
"payment_link_id": null,
"order_description": "21882 - SAR"
}
],
{
"data_count": 70
}
]
这是我的根pojo,我用字符串
解析它@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DownloadReportResponse {
private TransactionCount transactionCount;
private List<TransactionsResponse> transactions;
}
解析:
List<DownloadReportResponse> properties = new ObjectMapper().readValue(report, new TypeReference<>() {
});
扩展我的评论,你可以尝试这样做:
ObjectMapper om = new ObjectMapper();
//read the json into a generic structure
JsonNode tree = om.readTree(json);
//check if the top level element is an array
if(tree.isArray()) {
//iterate over the elements
tree.forEach(element -> {
//distinguish between nested list and object
if(element.isArray()) {
List<TransactionsResponse> responses = om.convertValue(element, new TypeReference<List<TransactionsResponse>>(){});
//do whatever needed with the list
} else if(element.isObject()) {
TransactionCount txCount = om.convertValue(element, TransactionCount .class);
//use the count as needed
}
});
}
这取决于您获得的数组包含TransactionsResponse
元素或TransactionCount
类型对象的内部数组,但没有其他内容。
然而,如果你有机会修改响应,我建议你拍摄这样的方式更容易解析和理解:
{
"transactions":[ ... ],
"transactionCount": {
"data_count": 70
}
}