Created
February 16, 2012 17:25
-
-
Save mandubian/1846587 to your computer and use it in GitHub Desktop.
Play2 Json sample correction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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