Java 将json字符串转换为包含@key的POJO

Java 将json字符串转换为包含@key的POJO,java,json,jackson,pojo,Java,Json,Jackson,Pojo,我有一个json字符串,如下所示 { "input_index": 0, "candidate_index": 0, "delivery_line_1": "5461 S Red Cliff Dr", "last_line": "Salt Lake City UT 84123-5955", "delivery_point_barcode": "841235955990" } public class Candidate { @Key("inpu

我有一个json字符串,如下所示

{
    "input_index": 0,
    "candidate_index": 0,
    "delivery_line_1": "5461 S Red Cliff Dr",
    "last_line": "Salt Lake City UT 84123-5955",
    "delivery_point_barcode": "841235955990"
}
public class Candidate {

    @Key("input_index")
    private int inputIndex;

    @Key("candidate_index")
    private int candidateIndex;

    @Key("addressee")
    private String addressee;

    @Key("delivery_line_1")
    private String deliveryLine1;

    @Key("delivery_line_2")
    private String deliveryLine2;

    @Key("last_line")
    private String lastLine;

    @Key("delivery_point_barcode")
    private String deliveryPointBarcode;
}
ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Candidate candidate = objectMapper.readValue(jsonString,Candidate.class);
我想转换成类的POJO,如下所示

{
    "input_index": 0,
    "candidate_index": 0,
    "delivery_line_1": "5461 S Red Cliff Dr",
    "last_line": "Salt Lake City UT 84123-5955",
    "delivery_point_barcode": "841235955990"
}
public class Candidate {

    @Key("input_index")
    private int inputIndex;

    @Key("candidate_index")
    private int candidateIndex;

    @Key("addressee")
    private String addressee;

    @Key("delivery_line_1")
    private String deliveryLine1;

    @Key("delivery_line_2")
    private String deliveryLine2;

    @Key("last_line")
    private String lastLine;

    @Key("delivery_point_barcode")
    private String deliveryPointBarcode;
}
ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Candidate candidate = objectMapper.readValue(jsonString,Candidate.class);
我正在尝试使用jackson将json转换为pojo,如下所示

{
    "input_index": 0,
    "candidate_index": 0,
    "delivery_line_1": "5461 S Red Cliff Dr",
    "last_line": "Salt Lake City UT 84123-5955",
    "delivery_point_barcode": "841235955990"
}
public class Candidate {

    @Key("input_index")
    private int inputIndex;

    @Key("candidate_index")
    private int candidateIndex;

    @Key("addressee")
    private String addressee;

    @Key("delivery_line_1")
    private String deliveryLine1;

    @Key("delivery_line_2")
    private String deliveryLine2;

    @Key("last_line")
    private String lastLine;

    @Key("delivery_point_barcode")
    private String deliveryPointBarcode;
}
ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Candidate candidate = objectMapper.readValue(jsonString,Candidate.class);
当我运行代码时,我在pojo中得到所有空值,因为jackson在json字符串中寻找属性名,而不是@key中给出的名称。如何告诉Jackson基于@Key映射值


我以前使用过@JsonProperty,在转换为pojo时没有问题。候选类由第三方提供,它们使用@key(com.google.api.client.util.key)注释作为属性。因此,我无法更改该类。

假设您无法更改该类,您也可以使用GSON将其转换回候选类。我之所以建议这样做,只是因为您无法更改POJO类中的注释

    Gson gson = new Gson();

    String jsonInString = "{\"input_index\": 0,\"candidate_index\": 0,\"delivery_line_1\": \"5461 S Red Cliff Dr\",\"last_line\": \"Salt Lake City UT 84123-5955\",\"delivery_point_barcode\": \"841235955990\"}";

    Candidate candidate = gson.fromJson(jsonInString, Candidate.class);

    System.out.println(candidate);
虽然这并不能取代您现有的JACKSON注释和对象映射器,但在本例中,您将在提供的源POJO中使用GSON

编辑您也可以使用JacksonFactory,如下所示

使用此maven dep:

<dependency>
    <groupId>com.google.http-client</groupId>
    <artifactId>google-http-client-jackson</artifactId>
    <version>1.15.0-rc</version>
</dependency>

请注意,在Jackson中,您可以使用
属性命名策略。CAMEL\u CASE\u TO\u LOWER\u CASE\u,带有下划线。
。感谢您的帮助chrylis。我使用这个PropertyName策略只得到了很少的价值。谢谢你的帮助易卜拉欣。实际上我有一份候选人名单。我正在做的是List candidates=Arrays.asList(new JacksonFactory().fromString(jsonInString,Candidate[].class));我得到了一个错误。您知道如何使用JacksonFactory转换对象列表吗?列表代替候选[]。类?您能给出完整的语句吗?@choom
ArrayList候选者=JacksonFactory.getDefaultInstance().fromString(输出,ArrayList.class)获取编译错误。列表无法解析为变量。候选项无法解析为变量。@choom我更新我的评论。应该是
ArrayList
谢谢你的帮助Ramachandran。实际上我有一份候选人名单。我正在做的是List candidates=Arrays.asList(new JacksonFactory().fromString(jsonInString,Candidate[].class));我得到了一个错误。您知道如何使用JacksonFactory转换对象列表吗?