我在用Jackson解析JSON时遇到了麻烦,我想忽略null属性。这是我的密码。
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class ParsedSurvey(
val items: List<ParsedSurveyItem> = listOf()
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class ParsedSurveyItem(
val type: String = "",
val text: String = "",
val instructions: String = "",
val prog: String = "",
val `var`: String = "",
val columns: List<ParsedSurveyAnswer> = listOf(),
val rows: List<ParsedSurveyAnswer> = listOf(),
val num: String = "",
val multi: Boolean = false,
val random: Boolean = false,
val min: Int = -1,
val max: Int = -1,
val recordOrder: Boolean = false,
val rowLength: Int = -1
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class ParsedSurveyAnswer @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) constructor(
val text: String = "",
val prog: String = "<p></p>",
@JsonProperty("isOpen") val isOpen: Boolean = false
)
如果我试图将ParsedSurveyItem中的rows属性设置为null。我得到了这个错误:
value failed for JSON property rows due to missing (therefore NULL) value for creator parameter rows which is a non-nullable type.
Jackson doesn't ignore
为什么Jackson解析空值?谢谢你帮忙。
发布于 2017-08-03 19:18:21
只有当rows属性为null
为空时,才能设置它。意思是把它改成
val rows: List<ParsedSurveyAnswer>?
您还可以删除listOf()
和@JsonIgnoreProperties(ignoreUnknown = true)
https://stackoverflow.com/questions/45478253
复制相似问题