我正试图利用cire在scala中进行json解析。你能帮我从case类中的数据中解析"pocs"吗?这是代码:
import io.circe.Decoder
import io.circe.generic.semiauto.deriveDecoder
import io.circe.parser
val json: String =
"""
{
"segmements": [
{
"tableName": "X",
"segmentName": "XX",
"pocs": [
"aa@aa.com",
"bb@bb.com"
]
},
{
"tableName": "Y",
"segmentName": "YY",
"pocs": [
"aa@aa.com",
"bb@bb.com"
]
}
]
}
"""
final case class TableInfo(tableName: String, segmentName: String)
object TableInfo {
implicit final val TableInfoDecoder: Decoder[TableInfo] = deriveDecoder
}
val result = for {
data <- parser.parse(json)
obj <- data.asObject.toRight(left = new Exception("Data was not an object"))
segmements <- obj("segmements").toRight(left = new Exception("Json didn't had the
segments key"))
r <- segmements.as[List[TableInfo]]
} yield r
println(result)
scastie链接:https://scastie.scala-lang.org/BalmungSan/eVEvBulOQwGzg5hIJroAoQ/3
只需添加类型为String
:集合的参数
final case class TableInfo(tableName: String, segmentName: String, pocs: Seq[String])
scastie