我有一个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"
}
我想转换成类的POJO,如下所示。
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;
}
我正在尝试使用杰克逊将json转换为pojo,如下所示。
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Candidate candidate = objectMapper.readValue(jsonString,Candidate.class);
当我运行代码时,我在pojo中获取了所有空值,因为杰克逊正在json字符串中寻找属性名称,而不是@key中给出的名称。如何告诉Jackson映射基于@Key的值?
我以前使用@JsonProperty,将其转换为pojo没问题。Candidate类由第三方提供,并且他们使用@key(com.google.api.client.util.Key)注释作为属性。因此,我无法更改课程。
使用这个Maven Dep:
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.15.0-rc</version>
</dependency>
并像这样转换:
Candidate candidate = JacksonFactory.getDefaultInstance().fromString(output,Candidate.class);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句