如何在lift项目中使用dispatch.json
我对如何在调度和提升中结合 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)
我不确定并且无法弄清楚下一步要做什么才能迭代数据,提取它并将其呈现到我的网页。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您返回的错误是让您知道
status
的类型既不是 String 也不是java.io.Reader
。相反,您拥有的是已解析的 JSON 响应的列表,因为 Dispatch 已经完成了将响应解析为 JSON 响应的所有艰苦工作。 Dispatch 有一个非常紧凑的语法,当您习惯它时这很好,但一开始它可能会非常迟钝,尤其是当您第一次接触 Scala 时。很多时候,您会发现在第一次学习时必须深入研究库的源代码才能了解发生了什么。例如,如果您查看dispatch-twitter源代码,您可以看到timeline
方法 实际上对响应执行 JSON 提取:该方法定义的是一个 Dispatch Handler,它转换 Response 对象转换为 JsonResponse 对象,然后将响应解析为 JSON 对象列表。一行中发生了很多事情。您可以在 JsHttp.scala 文件。 Dispatch 定义了许多处理程序,这些处理程序在幕后转换为不同类型的数据,然后您可以将这些数据传递给块来使用。查看 StdOut 演练 和 常见任务页面,但您需要深入研究各个模块的源代码或 Scaladoc 来查看还有哪些内容。
所有这些都是要达到您想要的效果的很长的路要走,我相信本质上是这样的:
您可以以任何您想要的方式将其推送到您的网页,而不是执行 println 。查看 Status 对象 用于一些预先构建的提取器,用于从状态响应中提取信息。
The error that you are getting back is letting your know that the type of
status
is neither a String orjava.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 thetimeline
method actually performs a JSON extraction on the response: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:
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.这是将 Dispatch HTTP 与 Lift-JSON 结合使用的另一种方法。此示例从 google 获取 JSON 文档,从中解析所有“标题”并打印它们。
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.