How to use dispatch.json in elevator project

I got confused on how to combine the json library in dispatching and raise to parse my json answer.

I am apparently a newbie to scala.

I wrote this code:

val status = { val httpPackage = http(Status(screenName).timeline) val json1 = httpPackage json1 } 

Now I am fixated on how to parse twitter json answer

I tried using JsonParser:

 val status1 = JsonParser.parse(status) 

but got this error:

 <console>:38: error: overloaded method value parse with alternatives: (s: java.io.Reader)net.liftweb.json.JsonAST.JValue<and> (s: String)net.liftweb.json.JsonAST.JValue cannot be applied to (http.HttpPackage[List[dispatch.json.JsObject]]) val status1 = JsonParser.parse(status1) 

I am not sure and cannot figure out what to do next in order to iterate over the data, extract it and display it on my web page.

+4
source share
2 answers

The error you return lets you know that the status type is neither a string nor java.io.Reader . Instead, you have a list of already parsed JSON responses, since Dispatch has already done all the hard work of parsing the response into a JSON response. The dispatcher has a very compact syntax, which is good when you are used to it, but it can be very dumb from the beginning, especially when you first approach Scala. Often you will find that you need to dive into the source code of the library when you first find out what happens. For example, if you look at the source code of the send-twitter, you will see that the timeline method actually performs JSON extraction in response

 def timeline = this ># (list ! obj) 

What this method defines is a dispatch manager, which converts the Response object to a JsonResponse object, and then parses the response in the list of JSON objects. This happens a little on one line. You can define the definition of the operand ># in the JsHttp.scala file in the http + json send module. The dispatcher defines a set of handlers that perform the conversion behind the scenes into different types of data, which can then be transferred to the block for work. Check out StdOut Walkthrough and Common Tasks for some handlers, but you will need to dive into the source code of various modules or Scaladoc to find out what else is there.

All this is a long way to get to what you want, which in my opinion is basically like this:

 val statuses = http(Status(screenName).timeline) statuses.map(Status.text).foreach(println _) 

Only instead of println you can push it to your web page in any way. Check the status object for some of the predefined extractors to extract information from the status response.

+5
source

Here is another way to use HTTP Send using Lift-JSON. This example retrieves a JSON document from Google, parses all the "headers" and prints them.

 import dispatch._ import net.liftweb.json.JsonParser import net.liftweb.json.JsonAST._ object App extends Application { val http = new Http val req = :/("www.google.com") / "base" / "feeds" / "snippets" <<? Map("bq" -> "scala", "alt" -> "json") val json = http(req >- JsonParser.parse) val titles = for { JField("title", title) <- json JField("$t", JString(name)) <- title } yield name titles.foreach(println) } 
+6
source

All Articles