I’ve been working through parsing .json files in a Play 2.0 project and there is one thing I can’t figure out. Here is a snippet from the online docs:
{
"users":[
{
"name": "Bob",
"age": 31.0,
"email": "bob@gmail.com"
},
{
"name": "Kiki",
"age": 25.0,
"email": null
}
]
}
What I want to know is, how do I grab one whole user? The problem is that I can’t figure out how to reference the grouping of parameters that represents a single user. I’ve tried something like
( json \\ "users" )
which just gives all the users as a single element in a list, and I’ve tried something like
( json \ "users" \ (user)(0))
but it seems I have to define ‘user’ and I have no idea what would be appropriate for that.
Better yet, is there a way to grab all the customers in a list? Or even just iterate over the tree and hit upon each user so I can access all the information of a specific user at once?
If you already know that the JSON contains a list of objects, you can ask for that element to be extracted as one, using
as[List[JsObject]].For example:
Generates:
Each of these list elements support the same operators as the original JSON object, so you can extract individual values using
(user \ "name").as[String], etc.