给出以下JSON ......
{
"firstName": "Joe",
"lastName": "Grey",
...
"addresses":
[
{
"name": "Default",
"street": "...",
...,
"isDefault": true
},
{
"name": "Home",
"street": "...",
...,
"isDefault": false
},
{
"name": "Office",
"street": "...",
...,
"isDefault": false
}
]
}
...如何让我们说name
等于Home
的项目?
{
"name": "Home",
"street": "...",
...,
"isDefault": false
}
感谢。
答案 0 :(得分:1)
user1502304建议的链接不提供有关此主题的任何信息。此外,根据this discussion,当前的JSON库有一些限制......如上所述here,良好的替代方案是JsZipper
。也就是说,下面是如何按值获取项目:
scala> import play.api.libs.json._
import play.api.libs.json._
scala> import play.api.libs.json.extensions._
import play.api.libs.json.extensions._
scala> val user = Json.parse("""
| {
| "firstName": "Joe",
| "lastName": "Grey",
| "isDefault": false,
| "addresses":
| [
| {
| "name": "Default",
| "street": "one",
| "isDefault": true
| },
| {
| "name": "Home",
| "street": "two",
| "isDefault": false
| },
| {
| "name": "Office",
| "street": "three",
| "isDefault": false
| }
| ]
| }""")
json: play.api.libs.json.JsValue = {"firstName":"Joe","lastName":"Grey","isDefault":false,"addresses":[{"name":"Default","street":"one","isDefault":true},{"name":"Home","street":"two","isDefault":false},{"name":"Office","street":"three","isDefault":false}]}
scala> JsZipper(json).findByValue(_ \ "name" == JsString("Default")).value
res8: play.api.libs.json.JsValue = {"name":"Default","street":"one","isDefault":true}
我希望它有所帮助。