Converting json to oher type should be easy. From the Play! documentation:
var str = "{\"next_cursor\":0,\"ids\":[123123,345345],\"previous_cursor\":0}"
var fol = Json.parse(str)
var fin = Json.fromJson[List[String]].fromJson(fol)
Should work without problem. It compiles fine, but failed with this error:
[RuntimeException: List expected]
Instead, this works:
var str = "{\"next_cursor\":0,\"ids\":[123123,345345],\"previous_cursor\":0}"
var fol = Json.parse(str)
var fin = (fol \ "ids") match {
case ids: JsArray => ids.value.map(_.toString)
case _ => JsArray()
}
Why? Am I understanding something wrong with the API? I’m trying this in PlayFramework 2.0.1.
You can’t parse this as a
List[String]directly, as it is a list of numbers, not strings. Your match example works because you map the ids to a string afterwards. Using your example, you’d write it more like:To make the syntax slightly easier to read, I’d make use of
JsValue.as[T], which is the equivalent ofJson.fromJson[T]:And if you need the ids to be converted to strings: