我在gent一個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
*/
現在,有時候,這個雙重值我得到作爲字符串的垃圾值。
那麼,如何在投射時忽略這些記錄。以下是我的鑄造代碼。
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"
}]
你能提供一個樣本JSON? – notionquest
你可以忽略字段(完全)在該字段使用@JsonIgnore註釋 – kuhajeyan
jsonIgnore將永遠忽略它寫入???但我想刪除數據類型不匹配時 – anij