Skip to content

Instantly share code, notes, and snippets.

@mandubian
Created February 16, 2012 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandubian/1846587 to your computer and use it in GitHub Desktop.
Save mandubian/1846587 to your computer and use it in GitHub Desktop.
Play2 Json sample correction
def index = Action {
import Json._
//val jsObj = Json( "alpha" \: 123 ++ "beta" \: "chboing" )
val data = JsObject(
Seq(("newspaper", JsObject(Seq(
("url", JsString("http://nytimes.com")),
("attributes", JsObject(Seq(
("name", JsString("nytimes")),
("country", JsString("US")),
("id", JsNumber(25))
))),
("links", JsArray(List(JsString("http://link1"), JsString("http://link2"))))
)))
))
case class Attributes(name: String, id: Int, country: String)
val attributes = toJson(data) \ "newspaper" \ "attributes"
val res = Attributes( (attributes \ "name") match {case JsString(name)=>name; case _ => ""},
(attributes \ "id") match {case JsNumber(id)=>id.toInt; case _ => 0},
(attributes \ "country") match {case JsString(country)=>country; case _ => ""})
Ok(res.toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment