Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
游戏框架&;JSON:如何通过值获取数组项_Json_Scala_Playframework - Fatal编程技术网

游戏框架&;JSON:如何通过值获取数组项

游戏框架&;JSON:如何通过值获取数组项,json,scala,playframework,Json,Scala,Playframework,给定以下JSON { "firstName": "Joe", "lastName": "Grey", ... "addresses": [ { "name": "Default", "street": "...", ..., "isDefault": true }, { "name": "Home", "street": "...", ..., "isDef

给定以下JSON

{
  "firstName": "Joe",
  "lastName": "Grey",
  ...
  "addresses":
  [
    {
      "name": "Default",
      "street": "...",
      ...,
      "isDefault": true
    },
    {
      "name": "Home",
      "street": "...",
      ...,
      "isDefault": false
    },
    {
      "name": "Office",
      "street": "...",
      ...,
      "isDefault": false
    }
  ]
}
。。。我该如何获得
名称
等于
主页
的项目

    {
      "name": "Home",
      "street": "...",
      ...,
      "isDefault": false
    }

谢谢。

用户1502304建议的链接不提供有关此主题的任何信息。此外,根据目前的JSON库有一些限制。。。如前所述,好的选择是
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}
我希望有帮助