如何在lift项目中使用dispatch.json

发布于 2024-09-20 00:47:45 字数 735 浏览 4 评论 0 原文

我对如何在调度和提升中结合 json 库来解析我的 json 响应感到困惑。

我显然是一个scala新手。

我写了这段代码:

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

现在我被困在如何解析 twitter json 响应上

,我尝试使用 JsonParser:

val status1 = JsonParser.parse(status) 

但收到此错误:

<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 confused on how to combine the json library in dispatch and lift to parse my json response.

I am apparently a scala newbie.

I have written this code :

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

Now i am stuck on how to parse the twitter json response

I've tried to use the 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 unsure and can't figure out what to do next in order to iterate through the data, extract it and render it to my web page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2024-09-27 00:47:52

您返回的错误是让您知道 status 的类型既不是 String 也不是 java.io.Reader。相反,您拥有的是已解析的 JSON 响应的列表,因为 Dispatch 已经完成了将响应解析为 JSON 响应的所有艰苦工作。 Dispatch 有一个非常紧凑的语法,当您习惯它时这很好,但一开始它可能会非常迟钝,尤其是当您第一次接触 Scala 时。很多时候,您会发现在第一次学习时必须深入研究库的源代码才能了解发生了什么。例如,如果您查看dispatch-twitter源代码,您可以看到 timeline 方法 实际上对响应执行 JSON 提取:

def timeline = this ># (list ! obj)

该方法定义的是一个 Dispatch Handler,它转换 Response 对象转换为 JsonResponse 对象,然后将响应解析为 JSON 对象列表。一行中发生了很多事情。您可以在 JsHttp.scala 文件。 Dispatch 定义了许多处理程序,这些处理程序在幕后转换为不同类型的数据,然后您可以将这些数据传递给块来使用。查看 StdOut 演练常见任务页面,但您需要深入研究各个模块的源代码或 Scaladoc 来查看还有哪些内容。

所有这些都是要达到您想要的效果的很长的路要走,我相信本质上是这样的:

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

您可以以任何您想要的方式将其推送到您的网页,而不是执行 println 。查看 Status 对象 用于一些预先构建的提取器,用于从状态响应中提取信息。

The error that you are getting back is letting your know that the type of status is neither a String or java.io.Reader. Instead, what you have is a List of already parsed JSON responses as Dispatch has already done all of the hard work in parsing the response into a JSON response. Dispatch has a very compact syntax which is nice when you are used to it but it can be very obtuse initially, especially when you are first approaching Scala. Often times, you'll find that you have to dive into the source code of the library when you are first learning to see what is going on. For instance, if you look into the dispatch-twitter source code, you can see that the timeline method actually performs a JSON extraction on the response:

def timeline = this ># (list ! obj)

What this method is defining is a Dispatch Handler which converts the Response object into a JsonResponse object, and then parses the response into a list of JSON Objects. That's quite a bit going on in one line. You can see the definition for the operand ># in the JsHttp.scala file in the http+json Dispatch module. Dispatch defines lots of Handlers that do a conversion behind the scenes into different types of data which you can then pass to block to work with. Check out the StdOut Walkthrough and the Common Tasks pages for some of the handlers but you'll need to dive into the various modules source code or Scaladoc to see what else is there.

All of this is a long way to get to what you want, which I believe is essentially this:

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

Only instead of doing a println, you can push it out to your web page in whatever way you want. Check out the Status object for some of the various pre-built extractors to pull information out of the status response.

凉栀 2024-09-27 00:47:51

这是将 Dispatch HTTP 与 Lift-JSON 结合使用的另一种方法。此示例从 google 获取 JSON 文档,从中解析所有“标题”并打印它们。

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)
}

Here's another way to use Dispatch HTTP with Lift-JSON. This example fetches JSON document from google, parses all "titles" from it 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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文