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轉換成模型)JsPath在斯卡拉播放Json
一個行太畝ch boilerplate ?? – 2014-10-07 13:11:39
您不認爲第一種方法(可以在XML XPath中工作)比第二種方法更簡單?這只是一個簡單的例子,還有更復雜的路徑查詢... – 2014-10-07 13:16:34