英文:
Jackson @JsonDeserialize not working on field
问题
@Data
public class EventOddPOJO {
@JsonProperty("id")
private String id;
@JsonDeserialize(using = EventOddPartDeserializer.class)
private String part;
@JsonProperty("ss")
private String score;
@JsonDeserialize(using = EventOddMinuteDeserializer.class)
private String minute;
@JsonDeserialize(using = EventOddSecondDeserializer.class)
private String second;
@JsonProperty("over_od")
private String overOd;
@JsonProperty("home_od")
private String homeOd;
@JsonProperty("draw_od")
private String drawOd;
@JsonProperty("away_od")
private String awayOd;
@JsonProperty("under_od")
private String underOd;
@JsonProperty("time_str")
private String timeStr;
@JsonProperty("add_time")
private String addTime;
@JsonProperty("handicap")
private String handicap;
public static class EventOddPartDeserializer extends StdDeserializer<String> {
public EventOddPartDeserializer() {
this(null);
}
public EventOddPartDeserializer(Class<?> vc) {
super(vc);
}
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
String timeStr = p.getValueAsString("timeStr");
if (Objects.isNull(timeStr)) {
return "-";
}
return timeStr.split(" - ")[0];
}
}
public static class EventOddMinuteDeserializer extends StdDeserializer<String> {
public EventOddMinuteDeserializer() {
this(null);
}
public EventOddMinuteDeserializer(Class<?> vc) {
super(vc);
}
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
String timeStr = p.getValueAsString("timeStr");
if (Objects.isNull(timeStr)) {
return "-";
}
return timeStr.split(" - ")[1].split(":")[0];
}
}
public static class EventOddSecondDeserializer extends StdDeserializer<String> {
public EventOddSecondDeserializer() {
this(null);
}
public EventOddSecondDeserializer(Class<?> vc) {
super(vc);
}
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
String timeStr = p.getValueAsString("timeStr");
if (Objects.isNull(timeStr)) {
return "-";
}
return timeStr.split(" - ")[1].split(":")[1];
}
}
}
Json:
{
"id": "35008186",
"home_od": "1.952",
"handicap": "+3.5",
"away_od": "1.740",
"ss": "95:98",
"time_str": "4 - 04:08",
"add_time": "1603807890"
}
英文:
@JsonDeserialize
not working on field.
Using Spring Boot 2.
Code:
@Data
public class EventOddPOJO {
@JsonProperty("id")
private String id;
@JsonDeserialize(using = EventOddPartDeserializer.class)
private String part;
@JsonProperty("ss")
private String score;
@JsonDeserialize(using = EventOddMinuteDeserializer.class)
private String minute;
@JsonDeserialize(using = EventOddSecondDeserializer.class)
private String second;
@JsonProperty("over_od")
private String overOd;
@JsonProperty("home_od")
private String homeOd;
@JsonProperty("draw_od")
private String drawOd;
@JsonProperty("away_od")
private String awayOd;
@JsonProperty("under_od")
private String underOd;
@JsonProperty("time_str")
private String timeStr;
@JsonProperty("add_time")
private String addTime;
@JsonProperty("handicap")
private String handicap;
public static class EventOddPartDeserializer extends StdDeserializer < String > {
public EventOddPartDeserializer() {
this(null);
}
public EventOddPartDeserializer(Class < ? > vc) {
super(vc);
}
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
String timeStr = p.getValueAsString("timeStr");
if (Objects.isNull(timeStr)) {
return "-";
}
return timeStr.split(" - ")[0];
}
}
public static class EventOddMinuteDeserializer extends StdDeserializer < String > {
public EventOddMinuteDeserializer() {
this(null);
}
public EventOddMinuteDeserializer(Class < ? > vc) {
super(vc);
}
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
String timeStr = p.getValueAsString("timeStr");
if (Objects.isNull(timeStr)) {
return "-";
}
return timeStr.split(" - ")[1].split(":")[0];
}
}
public static class EventOddSecondDeserializer extends StdDeserializer < String > {
public EventOddSecondDeserializer() {
this(null);
}
public EventOddSecondDeserializer(Class < ? > vc) {
super(vc);
}
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
String timeStr = p.getValueAsString("timeStr");
if (Objects.isNull(timeStr)) {
return "-";
}
return timeStr.split(" - ")[1].split(":")[1];
}
}
}
Json:
{
"id": "35008186",
"home_od": "1.952",
"handicap": "+3.5",
"away_od": "1.740",
"ss": "95:98",
"time_str": "4 - 04:08",
"add_time": "1603807890"
}
答案1
得分: 1
以下是翻译好的内容:
不能同时为JSON
负载中的同一个值使用3种不同的反序列化程序。如果您需要将值解析为3个不同的属性,可以创建新的POJO
并在自定义反序列化程序中实现解析。
POJO
类:
@Data
class EventOddPOJO {
@JsonProperty("id")
private String id;
@JsonProperty("ss")
private String score;
@JsonProperty("over_od")
private String overOd;
@JsonProperty("home_od")
private String homeOd;
@JsonProperty("draw_od")
private String drawOd;
@JsonProperty("away_od")
private String awayOd;
@JsonProperty("under_od")
private String underOd;
@JsonDeserialize(using = EventOddPartDeserializer.class)
@JsonProperty("time_str")
private EventOddPart timeStr;
@JsonProperty("add_time")
private String addTime;
@JsonProperty("handicap")
private String handicap;
}
@Data
class EventOddPart {
private String part;
private String minute;
private String second;
}
自定义反序列化程序:
class EventOddPartDeserializer extends JsonDeserializer<EventOddPart> {
@Override
public EventOddPart deserialize(JsonParser p, DeserializationContext ctxt) throws IOException{
String timeStr = p.getValueAsString();
EventOddPart eop = new EventOddPart();
if (Objects.isNull(timeStr)) {
return eop;
}
String[] parts = timeStr.split(" - ");
String[] minSecs = parts[1].split(":");
eop.setPart(parts[0]);
eop.setMinute(minSecs[0]);
eop.setSecond(minSecs[1]);
return eop;
}
}
英文:
You can not use 3 different deserialisers at the same time for the same value in JSON
payload. If you need to parse value into 3 different properties you can create new POJO
and implement parsing in custom deserialiser.
POJO
-s:
@Data
class EventOddPOJO {
@JsonProperty("id")
private String id;
@JsonProperty("ss")
private String score;
@JsonProperty("over_od")
private String overOd;
@JsonProperty("home_od")
private String homeOd;
@JsonProperty("draw_od")
private String drawOd;
@JsonProperty("away_od")
private String awayOd;
@JsonProperty("under_od")
private String underOd;
@JsonDeserialize(using = EventOddPartDeserializer.class)
@JsonProperty("time_str")
private EventOddPart timeStr;
@JsonProperty("add_time")
private String addTime;
@JsonProperty("handicap")
private String handicap;
}
@Data
class EventOddPart {
private String part;
private String minute;
private String second;
}
Custom deserialiser:
class EventOddPartDeserializer extends JsonDeserializer<EventOddPart> {
@Override
public EventOddPart deserialize(JsonParser p, DeserializationContext ctxt) throws IOException{
String timeStr = p.getValueAsString();
EventOddPart eop = new EventOddPart();
if (Objects.isNull(timeStr)) {
return eop;
}
String[] parts = timeStr.split(" - ");
String[] minSecs = parts[1].split(":");
eop.setPart(parts[0]);
eop.setMinute(minSecs[0]);
eop.setSecond(minSecs[1]);
return eop;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论