KotlinSpring Boot Jackson鞋

jm2pwxwz  于 7个月前  发布在  Kotlin
关注(0)|答案(1)|浏览(39)

我想将接收字符串转换为我的实体,但我有这个例外:com.fasterxml.Jackson.databind.exc.MismatchedInputException:无法从数组值(标记JsonToken.START_ARRAY)中解析fonbet.parser.models.childs.AllScore类型的值我做错了什么?我应该用什么对象来实现这个JSON?
我接收到JSON字符串,其中包含对象:

"liveEventInfos": [
    {
      "eventId": 42183931,
      "timer": "45:00",
      "timerSeconds": 2700,
      "timerDirection": 0,
      "scoreFunction": "Football",
      "scoreComment": "(1-2) https://www.youtube.com/watch v=TDkuCAfGoLg",
      "scoreCommentTail": "https://www.youtube.com/watch?v=TDkuCAfGoLg",
      "scores": [
        [
          {
            "c1": "1",
            "c2": "2"
          }
        ],
        [
          {
            "c1": "1",
            "c2": "2",
            "title": "тайм"
          },
          {
            "c1": "0",
            "c2": "0",
            "title": "тайм"
          }
        ]
      ],
      "subscores": []
    } 
]

我所拥有的:
我有这个JSON部分的实体:

data class LiveEventInfo(
    var eventId: Long?,
    var timer: String?,
    var timerSeconds: Long?,
    var timerDirection: Long?,
    var scoreFunction: String?,
    var scoreComment: String?,
    var scoreCommentTail: String?,
    var scores: List<AllScore>?
)

AllScore:

data class AllScore(
    val matchSore: HashMap<String, String>,
    val timeScore: HashMap<String, String>
)
py49o6xq

py49o6xq1#

几件事

  1. AllScore是一个有两个字段matchSoretimeScore的对象,这两个字段在JSON中没有引用。
  2. JSON流在节点scores下的外部数组中有1个或2个数组。解析器应该如何实现这一点?第一个是matchSore吗?如果是2,那么它是matchSoretimeScore
    1.您缺少subscores
    1.您提供的JSON缺少外部{}
    1.通常,在这种情况下,您应该有一个 Package 器对象来读取。
    下面的代码可以工作,你应该能够从这一点得到一些工作:
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule

fun main(args: Array<String>) {

    val json ="""
    {
        "liveEventInfos": [
        {
          "eventId": 42183931,
          "timer": "45:00",
          "timerSeconds": 2700,
          "timerDirection": 0,
          "scoreFunction": "Football",
          "scoreComment": "(1-2) https://www.youtube.com/watch v=TDkuCAfGoLg",
          "scoreCommentTail": "https://www.youtube.com/watch?v=TDkuCAfGoLg",
          "scores": [
            [
              {
                "c1": "1",
                "c2": "2"
              }
            ],
            [
              {
                "c1": "1",
                "c2": "2",
                "title": "тайм"
              },
              {
                "c1": "0",
                "c2": "0",
                "title": "тайм"
              }
            ]
          ],
          "subscores": []
        } 
    ]
    }
    """.trimIndent()

    val mapper = JsonMapper.builder()
        .addModule(KotlinModule.Builder().configure(KotlinFeature.StrictNullChecks, true).build())
        .build()
    val parsed = mapper.readValue(json, LiveEventInfos::class.java)
    println(parsed)
}

data class LiveEventInfos(
    val liveEventInfos: List<LiveEventInfo>
)

data class LiveEventInfo(
    var eventId: Long?,
    var timer: String?,
    var timerSeconds: Long?,
    var timerDirection: Long?,
    var scoreFunction: String?,
    var scoreComment: String?,
    var scoreCommentTail: String?,
    var scores: List<List<Map<String, String>>>?,
    val subscores: List<Any>?,
)

相关问题