如何在Kotlin中手动解析JSON?
我想在Kotlin中手动解析JSON响应。 由于我正在得到复杂的JSON响应,这是有一些共同的领域。 例如,我得到低于答复。
{ status: "success/false" apiId: 6 message: "Error msg if any" . . . // Here comes some JSON with complex structure where some fields are . // missing/omitted. Sometime array is missing/response in array is . // getting changed, getting lot of unwanted stuff Or whatever you can . // think. And because of this I need to parse it manually. . }
现在我怎么能在Kotlin的data
类中手动解析这种types的响应呢? 还有一件事我想知道,我可以在响应中使用任何基类的公共字段吗?
应该是这样的:
data class Response(val status: String, val apiId: Int , val message: String)
jackson-databind有@JsonIgnoreProperties(ignoreUnknown=true)
注释,可以忽略不需要的json字段。 那么你需要做的是写下面的数据类
@JsonIgnoreProperties(ignoreUnknown=true) data class Response(val status: String, val apiId: Int, val message: String)