如何在 Scala 中使用 Circe 将嵌套的 json 解码为 List[Object]

How to decode a nested json into a List[Object] using Circe in Scala

我有一个 json 看起来有点像这样:

{
    "results": {
        "result_1": {
            "text": "abc"
        },
        "result_2": {
            "text": "def"
        }
    }
}

我希望将其解码为 class 看起来像这样的案例:

case class Results(results: List[SingleResult])

case class SingleResult(text: String)

我想为此编写一个解码器,但我当前的解码器无法编译。它看起来像这样:

implicit val singleResultDecoder: Decoder[SingleResult] = deriveDecoder[SingleResult]
implicit val ResultsDecoder: Decoder[Results] = new Decoder[Results] {
    override def apply(c: HCursor): Result[Results] = {
        for {
            results <- c.keys.map(_.map(k => c.downField(k).as[SingleResult]).toList)
        } yield Results(results = results)
    }
}

编译错误为:

Type mismatch : 
Required: scala.List[SingleResult]
Found: scala.List[Result[SingleResult]]

任何关于如何修复错误的指导都将不胜感激。

你在这里混合了一些东西。在您的 json 中,您没有列表,因为您正在尝试阅读它。你有一张地图。因此我们需要相应地 Decoder:

implicit val ResultsDecoder: Decoder[Results] = new Decoder[Results] {
  override def apply(c: HCursor): Result[Results] = {
    for {
      results <- c.downField("results").as[Map[String, SingleResult]]
    } yield Results(results = results.values.toList)
  }
}

然后调用:

val decodedFoo = circe.jawn.decode[Results](jsonString)
println(decodedFoo)

结果:

Right(Results(List(SingleResult(abc), SingleResult(def))))

Scastie 中的代码 运行。