-
-
Save andypetrella/1865780 to your computer and use it in GitHub Desktop.
Test Play REPL
This file contains hidden or 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
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) |
This file contains hidden or 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
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