将 json 字符串转换为包含 @key 的 POJO
convert json string into POJO containing @key
我有一个 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"
}
我想转换成class的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;
}
我正在尝试使用 jackson 将 json 转换为 pojo,如下所示。
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没问题。 Candidate class 由第三方提供,他们对属性使用 @key(com.google.api.client.util.Key) 注释。所以,我无法更改 class.
假设您无法更改 class ,您也可以使用 GSON 将其转换回候选 class。我建议只 因为您不能更改您拥有的 POJO class 中的注释。
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 注释和对象映射器,但是在这种情况下,您在提供的 Source POJO 上几乎涵盖了 GSON
编辑 您也可以使用 JacksonFactory,如下所示
import com.google.api.client.json.jackson.JacksonFactory;
Candidate candidate2 = new JacksonFactory().fromString(jsonInString, Candidate.class);
System.out.println(candidate2);
使用这个 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);
我有一个 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"
}
我想转换成class的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;
}
我正在尝试使用 jackson 将 json 转换为 pojo,如下所示。
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没问题。 Candidate class 由第三方提供,他们对属性使用 @key(com.google.api.client.util.Key) 注释。所以,我无法更改 class.
假设您无法更改 class ,您也可以使用 GSON 将其转换回候选 class。我建议只 因为您不能更改您拥有的 POJO class 中的注释。
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 注释和对象映射器,但是在这种情况下,您在提供的 Source POJO 上几乎涵盖了 GSON
编辑 您也可以使用 JacksonFactory,如下所示
import com.google.api.client.json.jackson.JacksonFactory;
Candidate candidate2 = new JacksonFactory().fromString(jsonInString, Candidate.class);
System.out.println(candidate2);
使用这个 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);