2

我正在尝试将一个字符串(带有 json 值)映射到我的 POJO,但是当我这样做时得到一个空值。我能得到一些关于我做错了什么的建议吗?从我所看到的来看,它们是正确匹配的。

我有以下字符串:

"{\"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\"}]}]}"

它将符合以下结构。

{
"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"
            }
        ]
        }
    ]
}

这是我映射时输出为空的代码。

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

这些是我匹配以上字符串的 POJO。

@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;
}
4

2 回答 2

2

代替

ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

编写以下代码

ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
于 2019-09-18T16:47:02.723 回答
1

大多数情况下,JSON 中的字段与类中的字段不匹配。

因此,您必须确定 Jackson 的字段映射。

Jackson 提供了一种方法来识别 JSON 中的字段名称并将其与 Java 类中的字段相关联;@JsonProperty注释。

这是一些示例代码:

@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;
}
于 2019-09-18T16:47:26.240 回答