ナザール:
私は、次のようなJSONオブジェクトを持っています
{
"donor": "Y",
"bloodType": null,
"eligibility": {
"categoryEligible": false,
"suspensionEligible": false,
"paidFinesEligible": false,
"pointSystemEligible": false,
"failedDocuments": [
{
"type": "SOMETHING",
"reason": "SOMETHING_ELSE"
}
],
"eligible": false,
}
}
私は私のドメインオブジェクトに変換するためにジャクソンを使用しています。ここで私が使用しているフィールドは、次のとおりです。
private String donor;
@JsonProperty("eligibility")
private Eligibility eligibility;
資格クラスは、すべてのこれらのフィールドは、私がして、代わりにすべてのブール値のための個々のフィールドを持つのにしたい含まれている単一地図を持っている文字列プロパティ名とboolean型である<文字列、Bolean>は値です。
@JsonProperty("failedDocuments")
private List<FailedDocumentsItem> failedDocuments;
@JsonProperty("eligible")
private boolean eligible;
@JsonProperty("donor")
private boolean donor;
アンドレアス:
追加@JsonAnySetter
フィールド(ジャクソン2.8+)またはメソッドを:
マーカー論理「任意セッター」ミューテータを定義するために使用することができるアノテーション-のいずれかで非静的2引数法(タイプの(プロパティの最初の引数名、セットへの第2の値)またはフィールド使用
Map
またはPOJO)を-であることがすべてのそれ以外の場合は認識されない性質のために、「フォールバック」のハンドラは、JSONコンテンツから見つかっとして使用。
簡潔にするためにpublicフィールドを用いて、実施例。
public class Test {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(new File("test.json"), Root.class);
System.out.println("donor = " + root.donor);
System.out.println("flags = " + root.eligibility.flags);
System.out.println("failedDocuments = " + root.eligibility.failedDocuments);
}
}
class Root {
public Boolean realId;
public String donor;
public Boolean bloodType;
public Boolean selectiveServiceCandidate;
public Eligibility eligibility;
}
class Eligibility {
@JsonAnySetter
public Map<String, Boolean> flags = new HashMap<>();
public List<FailedDocument> failedDocuments;
}
class FailedDocument {
public String type;
public String reason;
@Override
public String toString() {
return "FailedDocument[type=" + this.type + ", reason=" + this.reason + "]";
}
}
出力
donor = Y
flags = {paidFinesEligible=false, hasRealId=false, suspensionEligible=false, acaaEligible=false, eligibleIgnoreRenewalDate=false, eligibleDocuments=false, cardStatusEligible=false, expirationDateEligible=false, eligible=false, citizenEligible=false, pointSystemEligible=false, ageEligible=false, gravamenesEligible=false, categoryEligible=false, eligibleMedical=false}
failedDocuments = [FailedDocument[type=CERTIFICATE_CITIZENSHIP, reason=MISSING]]