Reputation: 449
I'm completely new to Scala and Play and i stumbled upon the following the problem:
Given the following JSON structure:
[
{
"name": "Adam",
"age": 19
},
{
"name": "Berta",
"age": 22
},
...
]
I would like to map this JSON to a case classes like this:
case class User(name: String, age: Int)
case class Users(users: Seq[User])
or at least something like Seq[User]
.
I don't know how to traverse the JsPath because there is no key.
I tried to define an implicit read but either he cannot resolve the symbol "read" or he cannot found an implicit for user.
object User {
implicit val reads: Reads[User] = Json.reads[User]
}
object Users {
implicit val usersReads: Reads[Users] = (
(JsPath).read[Seq[User]]
)(Users.apply _)
}
How can I map my JSON to a working model?
Upvotes: 0
Views: 923
Reputation: 66
Something like this will work
import play.api.libs.json._
case class User(name: String, age: Int)
case class Users(users: Seq[User])
object User {
implicit val reads = Json.reads[User]
}
object Users {
implicit val reads: Reads[Users] = Reads {
_.validate[Seq[User]].map(Users(_))
}
}
Upvotes: 3