Created
November 26, 2012 14:53
-
-
Save propensive/4148607 to your computer and use it in GitHub Desktop.
Rapture I/O JSON extraction example
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
import rapture.io._ | |
// Let's parse some JSON | |
val src: Json = Json.parse(""" | |
{ | |
"foo": "Hello world", | |
"bar": { | |
"baz": 42 | |
} | |
} | |
""") | |
// This is the same as the JSON string literal: | |
val src: Json = json""" | |
{ | |
"foo": "Hello world", | |
"bar": { | |
"baz": 42 | |
} | |
} | |
""" | |
// We can now access the value bar.baz | |
val x: Json = src.bar.baz | |
// And get it as an integer | |
val y: Int = x.get[Int] | |
// Alternatively, we can use an extractor to get the values we want: | |
val json""" { "bar": { "baz": $x }, "foo": $z }""" = src | |
// Now x = 42 and z = "Hello world". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment