斯卡拉:播放 - 以JSON
问题描述:
算子节点
我JSON作为斯卡拉:播放 - 以JSON
val json =
{
"header" : "header value"
"value" : [
{
"a" : "a_val"
"b" : "b_val"
"c" : "c_val"
},
{
"a" : "a_val"
"b" : "b_val"
"c" : "c_val"
}
]
}
我想计算的记录
-
数,即2,我算
(json \\ "value")(0).asOpt[JsArray].map(_.value.size).getOrElse(0)
泰迪厄斯,但工程。简单的解决方案需要
在
value
子节点的数量,即3(a, b and c
)
不知道怎么算这个
编辑: 使用丹尼尔斯解决方案:我简单的记录数作为
(oDataJson \ "value").as[JsArray].value.size
答
(json \\ "value").headOption match {
case Some(JsArray(values)) => values.length
case _ => 0
}
// => 2
(json \\ "value").headOption match {
case Some(JsArray(Seq(JsObject(fields), _*))) => fields.size
case _ => 0
}
// => 3
+0
谢谢丹尼尔。它确实给出了预期的结果。 – kaur
你使用哪个json库? – Reactormonk
如果孩子的节点有不同的尺寸怎么办?例如其中一个没有'c'? –
@Reactormonk ..我使用播放框架 – kaur