Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Created February 19, 2012 21:06
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 andypetrella/1865780 to your computer and use it in GitHub Desktop.
Save andypetrella/1865780 to your computer and use it in GitHub Desktop.
Test Play REPL
scala> val o:JsObject = JsObject(Seq("a" -> JsNumber(1), "2" -> JsString("two")))
o: play.api.libs.json.JsObject = {"a":1.0,"2":"two"}
scala> o \ "a" //return the value as a JsValue
res26: play.api.libs.json.JsValue = 1.0
scala> val o2:JsObject = JsObject(Seq("a" -> JsNumber(1), "2" -> JsObject(Seq("deep" -> JsBoolean(true)))))
o2: play.api.libs.json.JsObject = {"a":1.0,"2":{"deep":true}}
scala> o \\ "a" // find at first level and return in a list
res27: Seq[play.api.libs.json.JsValue] = List(1.0)
scala> o \\ "deep" //won't find
res28: Seq[play.api.libs.json.JsValue] = List()
scala> o2 \\ "deep" // find one, adding it the the returned list
res29: Seq[play.api.libs.json.JsValue] = List(true)
val ar:JsArray = JsArray(List(JsString("a"), JsString("b"), JsString("c")))
//... or
// val ar:JsArray = JsArray(List("a","b","c").map(JsString(_)))
val listOfStrings = ar.as[List[JsValue]] // this uses the Format (Reads) that I'll talk brievly further
//... or simply
// val listOfStrings = ar.value
//show items
listOfStrings.map { case a:JsString => println(a.value) }
//using Format
listOfStrings.map { println(_.as[String]) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment