需要将多种类型的 JSON 响应映射到单个 POJO,以便我可以比较不同的对象以深入了解差异。
我曾尝试将第一个响应映射到 POJO 并解析第二个响应以填充定义的 POJO:
class XXX {
@JsonProperty("accountHolder")
private String accountHolder;
@JsonProperty("routingNumber")
private String routingNumber;
@JsonProperty("balance")
private List<Balance> balance;
@JsonProperty("accountName")
private String accountName;
@JsonProperty("bankTransferCodeType")
private String bankTransferCodeType;
@JsonProperty("individualInformation")
private IndividualInformation individualInformation;
@JsonProperty("acctType")
private String acctType;
@JsonProperty("transactionList")
private TransactionList transactionList;
@JsonProperty("accountNumber")
private String accountNumber;
@JsonProperty("uniqueId")
private String uniqueId;
@JsonProperty("bankNetID")
private String bankNetID;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
}
第一 react :
[
{
"ACCOUNT_NAME": "",
"ACCOUNT_NUMBER": "",
"AVAILABLE_BALANCE": null,
"CURRENT_BALANCE": "",
"FULL_ACCOUNT_NUMBER": null,
}
]
第二个回复:
"bankAccount": [
{
"accountName": "",
"accountNumber": "",
"routingNumber": "",
"fullAccountNumber": "",
"bankTransferCodeType": "",
"acctType": "",
"transactionList": {
"transaction": [
{
"amount": {
"curCode": "",
"content": ""
}
],
"oldestTxnDate": ""
},
"uniqueId":
}
}
期待一种通用的方法来将不同的结构化 JSON 实体映射到单个 POJO。
最佳答案
How to map multiple JSON responses to a single Java POJO?
由于两个响应似乎彼此完全不同,没有任何共同点,因此我不会尝试使用一个类来读取两个响应。
Expecting a generic way to map the different structured JSONs to single POJO.
您可以将这两个响应解析为
Map<String, Object>
然后将这些值映射到一个公共(public)类。您可以创建单独的类来映射每个响应。它将允许您根据需要分离它们和发展它们。您也可以使用 mapping frameworks例如MapStruct用于减少从一个对象映射到另一个对象时的样板代码。
关于java - 如何将多个 JSON 响应映射到单个 Java POJO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57452791/