无法使用杰克逊在Kotlin中解析json

时间:2017-12-16 21:11:41

标签: json jackson kotlin

我从服务器

返回了下一个json
{"s":2,"v":[[{"g":"value"},{"s":"value"},{"t":"value"}],[{"g":"value"},{"s":"value"},{"t":"value"}]]}

我在解析这个时遇到错误,所以我应该如何构建我的类? 现在我有:

companion object {
    class ResponseDataModel {
        @JsonProperty("s", required = true) var s: Int = 0
        @JsonProperty("v", required = true) lateinit var v: Array<SubjectsValueResponseDataModel>
    }

    class ValueResponseDataModel {
        @JsonProperty("s", required = true) lateinit var s: String
        @JsonProperty("t", required = true) lateinit var t: String
    }
}

忽略值g

    val mapper = ObjectMapper().registerKotlinModule()
    var respObject: DataModel.Companion.ResponseDataModel? = null
    try {
        val reader = BufferedReader(InputStreamReader(iStr, "UTF-8"), 8)
        val respContent = reader.readLine()
        Log.d("Response Data", respContent)
        respObject = mapper.readValue(respContent, DataModel.Companion.ResponseDataModel::class.java)
    } catch (e: Exception) {
        Log.e("Response Data", "Error converting result " + e.toString())
    } finally {
        iStr.close()
        return respObject
    }

0 个答案:

没有答案