This is my Cout object:
case class Cout (idCout:Int, cout:String)
object Cout{
implicit object CoutFormat extends Format[Cout] {
def reads(json: JsValue): Cout = Cout(
(json \ "idCout").as[Int],
(json \ "cout").as[String]
)
def writes(s: Cout): JsValue = JsObject(Seq(
"id" -> JsNumber(s.idCout),
"cout" -> JsString(s.cout)
))
}
}
I’m trying to use this class when calling a webservice using WS:
val cout = response.json.as[Cout]
But the scala compiler keeps complaining:
[RuntimeException: Int expected]
on the line (json \ "idCout").as[Int],
Could anybody tell me what I’m doing wrong?
- got the example from https://sites.google.com/site/play20zh/scala-developers/working-with-json
You have a typo in your
Formatinstance: you’re writing an"id"field but trying to read one named"idCout". Change one of them:And it works: