Created
January 13, 2011 08:56
-
-
Save ymnk/777605 to your computer and use it in GitHub Desktop.
pom.xml
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
% mvn scala:compile | |
% mvn scala:console | |
scala> LiftJson.json |
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
object LiftJson { | |
val json_string = | |
""" | |
{ | |
"created_at":"Wed Nov 18 18:54:12 +0000 2009", | |
"id":"5833943856", | |
"text":"RT @peoplemag: New Moon director Chris Weitz says he's quitting movies after one more http://ow.ly/DrjQ", | |
"source":"web", | |
"truncated":"false", | |
"in_reply_to_status_id":"", | |
"in_reply_to_user_id":"", | |
"favorited":"false", | |
"in_reply_to_screen_name":"", | |
"retweeted_status":{ | |
"created_at":"Wed Nov 18 18:36:34 +0000 2009", | |
"id":"5833513351", | |
"text":"New Moon director Chris Weitz says he's quitting movies after one more http://ow.ly/DrjQ", | |
"source":{ | |
"a":"HootSuite", | |
"href":"http://www.hootsuite.com", | |
"rel":"nofollow" | |
}, | |
"truncated":"false", | |
"in_reply_to_status_id":"", | |
"in_reply_to_user_id":"", | |
"favorited":"false", | |
"in_reply_to_screen_name":"", | |
"user":{ | |
"id":"25589776", | |
"name":"People magazine", | |
"screen_name":"peoplemag", | |
"location":"", | |
"description":"PEOPLE.com is the No. 1 site for celebrity news!", | |
"profile_image_url":"http://a3.twimg.com/profile_images/116213891/people_73x73_normal.jpg", | |
"url":"http://www.people.com", | |
"protected":"false", | |
"followers_count":"1653473", | |
"profile_background_color":"08a9e7", | |
"profile_text_color":"000000", | |
"profile_link_color":"ee0077", | |
"profile_sidebar_fill_color":"ffee9a", | |
"profile_sidebar_border_color":"ffcc66", | |
"friends_count":"406", | |
"created_at":"Fri Mar 20 22:30:24 +0000 2009", | |
"favourites_count":"2", | |
"utc_offset":"-18000", | |
"time_zone":"Eastern Time (US and Canada)", | |
"profile_background_image_url":"http://a1.twimg.com/profile_background_images/6859800/bgpage.gif", | |
"profile_background_tile":"true", | |
"statuses_count":"1740", | |
"notifications":"false", | |
"geo_enabled":"false", | |
"verified":"false", | |
"following":"false" | |
}, | |
"geo":"" | |
}, | |
"user":{ | |
"id":"44940026", | |
"name":"Anita Doller", | |
"screen_name":"testiverse", | |
"location":"", | |
"description":"", | |
"profile_image_url":"http://a3.twimg.com/profile_images/251164577/testing_normal.JPG", | |
"url":"", | |
"protected":"false", | |
"followers_count":"14", | |
"profile_background_color":"9ae4e8", | |
"profile_text_color":"000000", | |
"profile_link_color":"0000ff", | |
"profile_sidebar_fill_color":"e0ff92", | |
"profile_sidebar_border_color":"87bc44", | |
"friends_count":"25", | |
"created_at":"Fri Jun 05 17:07:09 +0000 2009", | |
"favourites_count":"1", | |
"utc_offset":"-28800", | |
"time_zone":"Pacific Time (US and Canada)", | |
"profile_background_image_url":"http://s.twimg.com/a/1258507899/images/themes/theme1/bg.png", | |
"profile_background_tile":"false", | |
"statuses_count":"14", | |
"notifications":"false", | |
"geo_enabled":"false", | |
"verified":"false", | |
"following":"true" | |
}, | |
"geo":"" | |
} | |
""" | |
import net.liftweb.json.JsonParser.parse | |
import net.liftweb.json.JsonAST.{JString, JField} | |
val json = parse(json_string) | |
val JField(_, JString(name)) = json \ "user" \ "name" | |
val JField(_, JString(screen_name)) = json \ "user" \ "screen_name" | |
val JField(_, JString(text)) = json \ "text" | |
import net.liftweb.json.JsonAST.render | |
import net.liftweb.json.Printer.pretty | |
val pretty_json_string = pretty(render(json)) | |
import net.liftweb.json.Xml.toXml | |
val xml = toXml(json) | |
// DSL | |
import net.liftweb.json.JsonAST._ | |
import net.liftweb.json.JsonDSL._ // implicit defitions | |
val json_list = List(1, 2, 3) | |
val json_compact = compact(render(json_list)) | |
} |
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.jcraft</groupId> | |
<artifactId>foo</artifactId> | |
<version>0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name></name> | |
<properties> | |
<scala.version>2.8.1</scala.version> | |
</properties> | |
<repositories> | |
<repository> | |
<id>scala-tools.org</id> | |
<name>Scala-Tools Maven2 Repository</name> | |
<url>http://scala-tools.org/repo-releases</url> | |
</repository> | |
</repositories> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>scala-tools.org</id> | |
<name>Scala-Tools Maven2 Repository</name> | |
<url>http://scala-tools.org/repo-releases</url> | |
</pluginRepository> | |
</pluginRepositories> | |
<dependencies> | |
<dependency> | |
<groupId>org.scala-lang</groupId> | |
<artifactId>scala-library</artifactId> | |
<version>${scala.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>net.liftweb</groupId> | |
<artifactId>lift-json_2.8.1</artifactId> | |
<version>2.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.scala-lang</groupId> | |
<artifactId>scala-compiler</artifactId> | |
<version>${scala.version}</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<sourceDirectory>src/main/scala</sourceDirectory> | |
<testSourceDirectory>src/test/scala</testSourceDirectory> | |
<plugins> | |
<plugin> | |
<groupId>org.scala-tools</groupId> | |
<artifactId>maven-scala-plugin</artifactId> | |
<executions> | |
<execution> | |
<goals> | |
<goal>compile</goal> | |
<goal>testCompile</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<scalaVersion>${scala.version}</scalaVersion> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
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
object ScalaJson { | |
val json_string = | |
""" | |
{ | |
"created_at":"Wed Nov 18 18:54:12 +0000 2009", | |
"id":"5833943856", | |
"text":"RT @peoplemag: New Moon director Chris Weitz says he's quitting movies after one more http://ow.ly/DrjQ", | |
"source":"web", | |
"truncated":"false", | |
"in_reply_to_status_id":"", | |
"in_reply_to_user_id":"", | |
"favorited":"false", | |
"in_reply_to_screen_name":"", | |
"retweeted_status":{ | |
"created_at":"Wed Nov 18 18:36:34 +0000 2009", | |
"id":"5833513351", | |
"text":"New Moon director Chris Weitz says he's quitting movies after one more http://ow.ly/DrjQ", | |
"source":{ | |
"a":"HootSuite", | |
"href":"http://www.hootsuite.com", | |
"rel":"nofollow" | |
}, | |
"truncated":"false", | |
"in_reply_to_status_id":"", | |
"in_reply_to_user_id":"", | |
"favorited":"false", | |
"in_reply_to_screen_name":"", | |
"user":{ | |
"id":"25589776", | |
"name":"People magazine", | |
"screen_name":"peoplemag", | |
"location":"", | |
"description":"PEOPLE.com is the No. 1 site for celebrity news!", | |
"profile_image_url":"http://a3.twimg.com/profile_images/116213891/people_73x73_normal.jpg", | |
"url":"http://www.people.com", | |
"protected":"false", | |
"followers_count":"1653473", | |
"profile_background_color":"08a9e7", | |
"profile_text_color":"000000", | |
"profile_link_color":"ee0077", | |
"profile_sidebar_fill_color":"ffee9a", | |
"profile_sidebar_border_color":"ffcc66", | |
"friends_count":"406", | |
"created_at":"Fri Mar 20 22:30:24 +0000 2009", | |
"favourites_count":"2", | |
"utc_offset":"-18000", | |
"time_zone":"Eastern Time (US and Canada)", | |
"profile_background_image_url":"http://a1.twimg.com/profile_background_images/6859800/bgpage.gif", | |
"profile_background_tile":"true", | |
"statuses_count":"1740", | |
"notifications":"false", | |
"geo_enabled":"false", | |
"verified":"false", | |
"following":"false" | |
}, | |
"geo":"" | |
}, | |
"user":{ | |
"id":"44940026", | |
"name":"Anita Doller", | |
"screen_name":"testiverse", | |
"location":"", | |
"description":"", | |
"profile_image_url":"http://a3.twimg.com/profile_images/251164577/testing_normal.JPG", | |
"url":"", | |
"protected":"false", | |
"followers_count":"14", | |
"profile_background_color":"9ae4e8", | |
"profile_text_color":"000000", | |
"profile_link_color":"0000ff", | |
"profile_sidebar_fill_color":"e0ff92", | |
"profile_sidebar_border_color":"87bc44", | |
"friends_count":"25", | |
"created_at":"Fri Jun 05 17:07:09 +0000 2009", | |
"favourites_count":"1", | |
"utc_offset":"-28800", | |
"time_zone":"Pacific Time (US and Canada)", | |
"profile_background_image_url":"http://s.twimg.com/a/1258507899/images/themes/theme1/bg.png", | |
"profile_background_tile":"false", | |
"statuses_count":"14", | |
"notifications":"false", | |
"geo_enabled":"false", | |
"verified":"false", | |
"following":"true" | |
}, | |
"geo":"" | |
} | |
""" | |
import util.parsing.json.JSON | |
val json = JSON.parse(json_string) | |
val json_raw = JSON.parseRaw(json_string) | |
val json_full = JSON.parseFull(json_string) | |
val screen_name = | |
json_full.get.asInstanceOf[Map[String,Any]]("user"). | |
asInstanceOf[Map[String,Any]]("screen_name"). | |
asInstanceOf[String] | |
import util.parsing.json.{JSONObject, JSONArray} | |
val name = json_raw.get match { | |
case JSONObject(map) => map("user") match { | |
case JSONObject(user) => user("name").asInstanceOf[String] | |
case JSONArray(_) => error("error") | |
} | |
case JSONArray(_) => error("error") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment