【发布时间】:2018-10-31 02:24:00
【问题描述】:
我有两个类似的类
import io.circe.Decoder
case class FactResponse(id: String, status: String) {
...
}
object FactResponse {
implicit val decoder: Decoder[FactResponse] =
Decoder.forProduct2("id", "status")(FactResponse.apply)
def apply(json: String): FactResponse = {
import io.circe.parser.decode
decode[FactResponse](json).right.get
}
}
case class RuleEngineRequestResponse(content: Seq[Map[String, String]])
object RuleEngineRequestResponse {
implicit val decoder: Decoder[RuleEngineRequestResponse] =
Decoder.forProduct1("content")(RuleEngineRequestResponse.apply(_: String))
def apply(json: String): RuleEngineRequestResponse = {
import io.circe.parser.decode
println("here")
print(json)
println(decode[RuleEngineRequestResponse](json).left.get)
decode[RuleEngineRequestResponse](json).right.get
}
}
我正在尝试解码一个看起来像这样的 json
{“内容”:[{“id”:“22”,“状态”:“22”]}
但是,我遇到了解码失败 DecodingFailure(String, downfield("content"))
我不确定这里出了什么问题,json 绝对是正确的,我什至尝试将内容解析为一系列地图,但我仍然一遍又一遍地得到同样的结果。关于如何使用 circe 将嵌套对象解析为数组的任何想法?
【问题讨论】:
-
一些最佳实践:导入应该始终位于文件的顶部。在谈论函数式编程时,
.right、.left和.get通常是个坏主意。