JsPath to Scala Play Json中的数组

时间:2014-10-07 12:29:43

标签: json scala playframework-2.0

import play.api.libs.json._

val json: JsValue = Json.parse("""
  {
    "name" : "Independence",
    "foundingFathers" : [ {
      "name" : {
        "first": "John",
        "last": "Adams"
      },
      "country" : "United States"
    }, {
      "name" : {
        "first": "Artur",
        "last": "Mas"
      },
      "country" : "Catalonia"
    } ]
  }
""")

val lastNames = json \ "foundingFathers \ "name" \ "last"
// this does not work. it fails with: JsUndefined('last' is undefined on object: JsUndefined('name' is undefined on object: [{"name":{"first":"John","last":"Adams"},"country":"United States"},{"name":{"first":"Artur","last":"Mas"},"country":"Catalonia"}]))

// this does work, but there is too much boilerplate
val lastNames = (json \ "foundingFathers").as[List[JsObject]].map(_ \ "name" \ "last")

如何在没有样板的情况下实现结果? (在这种情况下,我想使用JsValues,我不想使用读者将JsValue转换为模型)

2 个答案:

答案 0 :(得分:3)

您可以使用隐式类将此功能包装起来

implicit class JsValueWrapper(val j: JsValue) extends AnyVal {
  def \|(fieldName: String) = j match {
    case JsArray(value) => JsArray(value.map(_ \ fieldName))
    case _ => j \ fieldName
  }
}

scala> json \| "foundingFathers" \| "name" \| "first"
res19: play.api.libs.json.JsValue = ["John","Artur"]

答案 1 :(得分:2)

小改进,不确定它是否回答了你的问题

val lastNames = json \ "foundingFathers" \\ "name" map ( _ \ "last")

lastNames:Seq [play.api.libs.json.JsValue] = ListBuffer(“Adams”,“Mas”)