gpt4 book ai didi

java - Kotlin - Jackson 忽略空值

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:34 31 4
gpt4 key购买 nike

我在使用 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 中的行属性设置为 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 解析空值?感谢您的帮助。

最佳答案

您可以设置行属性 null 只有当它可以为 null 时。它的意思是改成

 val rows: List<ParsedSurveyAnswer>? 

您还可以删除 listOf() & @JsonIgnoreProperties(ignoreUnknown = true)

关于java - Kotlin - Jackson 忽略空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478253/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com