Created
June 13, 2012 04:33
-
-
Save takungsk/2921889 to your computer and use it in GitHub Desktop.
StackOverflowで見つけた 標準のライブラリで JSON 扱う サンプル
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
// JSON use std lib. | |
// StackOverflowで見つけた 標準のライブラリで JSON 扱う サンプル | |
// how-to-parse-json-in-scala-using-standard-scala-classes | |
// http://stackoverflow.com/questions/4170949/ | |
import scala.util.parsing.json._ | |
// jsonデータサンプル | |
val jsonString = """{"languages":[{"name":"japanese","is_active":true,"completeness": 1.0,"books":{"title":"title1","author":"neko"}},{"name":"english","is_active":true,"completeness": 2.0,"books":{"title":"title2","author":"Dog"}}]}""" | |
class CC[T] { def unapply(a:Any):Option[T] = Some(a.asInstanceOf[T]) } | |
object M extends CC[Map[String, Any]] | |
object L extends CC[List[Any]] | |
object S extends CC[String] | |
object D extends CC[Double] | |
object B extends CC[Boolean] | |
for { | |
Some(M(map)) <- List(JSON.parseFull(jsonString)) | |
L(languages) = map("languages") | |
M(language) <- languages | |
S(name) = language("name") | |
B(active) = language("is_active") | |
D(completeness) = language("completeness") | |
M(books) = language("books") | |
S(title) = books("title") | |
} yield { | |
(name, active, completeness, title) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment