Why am I unable to map my object to this String

karvai :

I am trying to map a String (with json values) to my POJO but getting a null when I do that. Can I get some advivec on what I am doing wrong pls. They are matching up correctly from what I see.

I have the following String:

"{\"identifier_type\":\"TEST\",\"simple_construct_response\":[{\"identifier\":\"123451234512435\",\"customer_id\":\"\",\"trim_code\":\"DDD\",\"trim_reason_code\":\"\",\"simple_products\":[{\"product_name\":\"ABC_CPS_ABCD\",\"product_presentment_timestamp\":\"2019-02-28 06:07:20:383\"}]}]}"

It would conform to the following structure.

{
"identifier_type": "TEST",
"simple_construct_response": [
    {
        "identifier": "123451234512435",
        "customer_id": "",
        "trim_code": "DDD",
        "trim_reason_code": "",
        "simple_products": [
            {
                "product_name": "ABC_CPS_ABCD",
                "product_presentment_timestamp": "2019-02-28 06:07:20:383"
            }
        ]
        }
    ]
}

This is my code where the output is null when I map.

String response = "{\"identifier_type\":\"TEST\",\"simple_construct_response\":[{\"identifier\":\"123451234512435\",\"customer_id\":\"\",\"trim_code\":\"DDD\",\"trim_reason_code\":\"\",\"simple_products\":[{\"product_name\":\"ABC_CPS_ABCD\",\"product_presentment_timestamp\":\"2019-02-28 06:07:20:383\"}]}]}";

    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MainResponse output = mapper.readValue(response, MainResponse.class); // this results in null

These are my POJOs to match above string.

@Getter
@Setter
public class MainResponse {
    private String identifierType;
    private List<SimpleConstructResponse> simpleConstructResponse;
}

@Getter
@Setter
public class simpleConstructResponse {
    private String identifier;
    private String customerId;
    private String trimCode;
    private String trimReasonCode;
    private List<SimpleProduct> simpleProducts;
}

@Getter
@Setter
public class SimpleProduct {
    private String productName;
    private String productPresentmentTimestamp;
}
DwB :

For the most part, the fields in your JSON do not match the fields in your class.

Because of this, you must identify the field mapping for Jackson.

Jackson provides a way to identify the field name in the JSON and to associate it with a field in the Java class; the @JsonProperty annotation.

Here is some example code:

@Getter
@Setter
public class MainResponse
{
    @JsonProperty("identifier_type")
    private String identifierType;

    @JsonProperty("simple_construct_response")
    private List<SimpleConstructResponse> simpleConstructResponseList;
}

@Getter
@Setter
public class SimpleConstructResponse
{
    private String identifier;

    @JsonProperty("customer_id")
    private String customerId;

    @JsonProperty("trim_code")
    private String trimCode;

    @JsonProperty("trim_reason_code")
    private String trimReasonCode;

    @JsonProperty("simple_products")
    private List<SimpleProduct> simpleProducts;
}

@Getter
@Setter
public class SimpleProduct
{
    @JsonProperty("product_name")
    private String productName;

    @JsonProperty("product_presentment_timestamp")
    private String productPresentmentTimestamp;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165148&siteId=1