杰克逊:如何忽略字段

时间:2016-11-15 09:41:46

标签: java json spring jackson

我在Spring Rest Template中设置json响应并尝试将其强制转换为自定义对象。

@JsonIgnoreProperties(ignoreUnknown = true)
public class DataItems {

private String deviceId;
    private String dataSourceName;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.ms+0000")
    private Date timestamp;
    private String mName;
    private String mVersion;
    private double value;


    /**
     * constructors and getters and setters
     */

现在,有时,这个双倍值我得到了String垃圾值。

那么,如何在投射时忽略这些记录。以下是我的演员代码。

ObjectMapper mapper = new ObjectMapper();
DataItems dataItems =  mapper.readValue(/*String*/json),DataItems.class);

示例Json:

[{
        "value": 26.394289,
        "deviceId": "97572545-3768-6070-0B09-1A566D8F6BC1",
        "dataSourceName": "AQM",
        "timestamp": "2016-11-04T05:24:55.000+0000",
        "mName": "SENS_TEMPERATURE",
        "mVersion": "1.0"
      },
      {
        "value": 26.396435,
        "deviceId": "97572545-3768-6070-0B09-1A566D8F6BC1",
        "dataSourceName": "AQM",
        "timestamp": "2016-11-04T05:25:58.000+0000",
        "mName": "SENS_TEMPERATURE",
        "mVersion": "1.0"
      },
      {
        "value": "{\"timestamp\": 1478220851, \"metric\": \"SENS_TAMPER\", \"value\": \"1\", \"source\": \"AQM\"}",
        "deviceId": "97572545-3768-6070-0B09-1A566D8F6BC1",
        "dataSourceName": "AQM",
        "timestamp": "2016-11-04T00:54:11.000+0000",
        "mName": "SENS_UNREGISTERED",
        "mVersion": "1.0"
      }]

2 个答案:

答案 0 :(得分:3)

自定义反序列化程序可以解决此问题。如果无法反序列化double值,则自定义反序列化器将设置默认值“0.0”

更改POJO: -

@JsonDeserialize(using = DoubleDeserializer.class)
private Double value;

DoubleDeserializer - 自定义反序列化程序: -

public class DoubleDeserializer extends JsonDeserializer<Double> {

    @Override
    public Double deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException, JsonProcessingException {

        String doubleValue = jsonParser.readValueAs(String.class);

        try {
            return Double.valueOf(doubleValue);
        } catch (NumberFormatException ne) {
            System.out.println("Default value is set as the value in json is not compatible with double value");
            return new Double(0);           
        }

    }
}

答案 1 :(得分:1)

您可以使用JsonNode类型代替double

@JsonIgnoreProperties(ignoreUnknown = true)
public class DataItems {
    private String   deviceId;
    private String   dataSourceName;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.ms+0000")
    private Date     timestamp;
    private String   mName;
    private String   mVersion;

    private JsonNode value;

    [...] // getters, setters
}

现在您可以尝试get a double from JsonNode-Object

double value = data.getValue().asDouble(0.0); // 0.0 is default

使用这种方式,您不会丢失其他非双数据:

{
    "value": "{\"timestamp\": 1478220851, \"metric\": \"SENS_TAMPER\", \"value\": \"1\", \"source\": \"AQM\"}",
    [...]
  }