我有一个很大的json对象:MyNesteObject
{
"size": 2,
"values": [{
"name": "mullock",
"upstatus": "Green",
"details": {
"key": "rupture farms",
"server": "mudos",
"owner": "magog_cartel",
"type": "NORMAL",
"links": {
"self": [{
"address": "https://mudos.com:port/access"
}]
}
}
},{
"name": "tassadar",
"upstatus": "Orange",
"details": {
"key": "archon",
"server": "protoss",
"owner": "aspp67",
"type": "NORMAL",
"links": {
"self": [{
"address": "https://aiur.com:port/access"
}]
}
}
}],
"limit": 100
}
case类如下所示:
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
case class Result(size: Int, limit: Int, values: Seq[Values])
case class Values(name: String, upstatus: String, details: ValuesDetails)
case class ValuesDetails(key: String, server: String, owner: String, `type`: String, links: ValuesDetailsLinks)
case class ValuesDetailsLinks(self: Seq[ValuesDetailsLinksAddress])
case class ValuesDetailsLinksAddress(address: String)
object Foo {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
def test(Json): Unit
val return = mapper.readValue[Result](Json)
myNestedObject。大小
是一个给出数组长度的字段
myNestedObject。价值观(一)
正在尝试将以下值提取到列表[字符串]
myNestedObject。值(i)。名称
我一直在使用一个粗略的for循环进行提取,代码正常工作。
val selectNames: List[String] = (
for (i <- 0 to myNestedObject.size-1 toList) yield
myNestedObject.values(i).name
)
重构代码并尝试使用。映射(未成功)执行相同操作,两次尝试:
myNestedObject.map(_ => values(i).name)
myNestedObject.(values(i).name).asInstanceOfList
免责声明:我在这方面完全是新手。
解决方案:无需指定索引即可访问值列表
myNestedObject.values.map(_.name)