Kotlin – 杰克逊忽略空值
我解析与杰克逊JSON的麻烦,我想忽略空属性。 这是我的代码。
@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中的行属性设置为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
为什么杰克逊分析空值? 感谢帮助。
只有在可为null
才能将行属性设置为null
。 这意味着改变它
val rows: List<ParsedSurveyAnswer>?
你也可以删除listOf()
& @JsonIgnoreProperties(ignoreUnknown = true)