[FIXED] Kann ich einen Java 17-Datensatz aus einer JSON-Nutzlast mit einem dynamischen Feld erstellen?

Ausgabe

Ich erhalte eine JSON-Nutzlast mit einem dynamischen Feld, aber ich muss sie in einen Datensatz umwandeln. Gibt es eine Möglichkeit, dies mit einem dynamischen Feld zu tun? Das betreffende Feld ist unter ‘ Items‘: ” CABC00033003“, ” CABC00033002“, ” CABC00033001

Nutzlast

{
    "Timestamp": "2022-09-15T08:35:15",
    "Agent": "JP",
    "AgentDevice": "",
    "Trace": "LOAD FOR DELIVERY",
    "Consignment": "CABC00033",
    "Items": {
        "CABC00033003": {
            "Type": "CONSIGNMENT",
            "Identifier": ""
        },
        "CABC00033002": {
            "Type": "CONSIGNMENT",
            "Identifier": ""
        },
        "CABC00033001": {
            "Type": "CONSIGNMENT",
            "Identifier": ""
        }
    },
    "FreightHandler": "",
    "Signature": "",
    "Comment": "",
    "Depot": null
}

Dies ist die Methode zum Akzeptieren einer JSON-Payload -> Record

public record CarrierABCStatusUpdateRecord(@JsonProperty("Timestamp") String timeStamp,
                                         @JsonProperty("Agent") String agent,
                                         @JsonProperty("AgentDevice") String agentDevice,
                                         @JsonProperty("Trace") String trace,
                                         @JsonProperty("Consignment") String consignment,
                                         @JsonProperty("Items") String items,
                                         @JsonProperty("FreightHandler") String freightHandler,
                                         @JsonProperty("Signature") String signature,
                                         @JsonProperty("Comment") String comment,
                                         @JsonProperty("Depot") String depot) {}

Lösung

Sie können Mapstattdessen so verwenden String.

public record TypeIdentifier(
    @JsonProperty("Type") String type,
    @JsonProperty("Identifier") String identifier) {
}

public record CarrierABCStatusUpdateRecord(
    @JsonProperty("Timestamp") String timeStamp,
    @JsonProperty("Agent") String agent,
    @JsonProperty("AgentDevice") String agentDevice,
    @JsonProperty("Trace") String trace,
    @JsonProperty("Consignment") String consignment,
//  @JsonProperty("Items") String items,
    @JsonProperty("Items") Map<String, TypeIdentifier> items,
    @JsonProperty("FreightHandler") String freightHandler,
    @JsonProperty("Signature") String signature,
    @JsonProperty("Comment") String comment,
    @JsonProperty("Depot") String depot) {
}


Beantwortet von –
英語は苦手


Antwort geprüft von –
Terry (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like