如何从dispatch.json.JsObject中提取

发布于 2024-09-24 13:40:11 字数 744 浏览 0 评论 0原文

我需要做什么来提取 Friends_count 的值。我注意到 screen_name 已经在 Status 对象和 case 类中定义。仍然需要以不同的方式扩展 Js 或 JsObject

object TweetDetails extends Js { val friends_count = 'friends_count ? num }

,然后将其与 JsObjects 列表中的每个 json 对象进行模式匹配,如下所示。这些符号令人困惑:

scala> val friends_count = 'friends_count ! num  // I wish SO understood Scala's symbols
val twtJsonList = http(Status("username").timeline)
twtJsonList foreach {
      js =>
        val Status.user.screen_name(screen_name) = js
        val Status.text(text) = js
        val friends_counts(friends_count) = js //i cannot figure out how to extract this
        println(friends_count)
        println(screen_name)
        println(text)

}

What do i need to do to extract the value for friends_count. i noticed that screen_name are already define in the Status object and case class. Do still require to extends Js or JsObject different

object TweetDetails extends Js { val friends_count = 'friends_count ? num }

and then pattern match it against each json object in the list of JsObjects as represented below. The symbols are confusing:

scala> val friends_count = 'friends_count ! num  // I wish SO understood Scala's symbols
val twtJsonList = http(Status("username").timeline)
twtJsonList foreach {
      js =>
        val Status.user.screen_name(screen_name) = js
        val Status.text(text) = js
        val friends_counts(friends_count) = js //i cannot figure out how to extract this
        println(friends_count)
        println(screen_name)
        println(text)

}

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

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

发布评论

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

评论(1

千と千尋 2024-10-01 13:40:11

通常,Scala 符号可以被认为是始终相同的唯一标识符。每个在词汇上相同的符号都指代完全相同的存储空间。从 Scala 的角度来看,它们没有什么特别之处。

然而,Dispatch-Json 会提取符号,使其成为 JSON 属性提取器。要查看负责拉皮条的代码,请查看 SymOp 类 和 JsonExtractor.scala 代码的其余部分。

让我们编写一些代码来解决您正在查看的问题,然后分析发生的情况:

trait ExtUserProps extends UserProps with Js {
  val friends_count = 'friends_count ! num 
}
object ExtUser extends ExtUserProps with Js

val good_stuff = for {
  item <- http(Status("username").timeline)
  msg = Status.text(item)
  user = Status.user(item)
  screen_name = ExtUser.screen_name(user)
  friend_count = ExtUser.friends_count(user)
} yield (screen_name, msg, friend_count)

我们要做的第一件事是扩展 Dispatch-Twitter 模块中的 UserProps 特征,为其提供 friends_count extractor,然后定义一个 ExtUser 对象,我们可以使用它来访问该提取器。因为 ExtUserProps 扩展了 UserProps,后者也扩展了 Js,所以我们在作用域中获得了方法 sym_add_operators,它将符号 'friends_count 转换为 SymOp 案例类。然后我们调用 <该 SymOp 上的 code>! 方法,然后我们将其传递给提取器 num to,它创建一个提取器,在 JSON 对象上查找属性“friends_count”,然后解析它作为返回之前的数字。对于这么小的代码来说,发生了相当多的事情。

程序的下一部分只是一个 for-compression,调用用户的 Twitter 时间线并将其解析为代表每个状态项的 JsObjects,我们应用 Status.text 提取器来提取它们输出状态消息。然后我们做同样的事情来拉出用户。然后,我们从用户 JsObject 中取出 screen_name 和friend_count,最后我们生成一个包含我们正在寻找的所有属性的 Tuple3。然后我们留下一个 List[Tuple3[String,String,BigDecimal]] ,然后您可以对其进行迭代以打印出来或执行任何操作。

我希望这能澄清一些事情。 Dispatch 库非常具有表现力,但可能有点难以理解,因为它使用了很多 Scala 技巧,而刚学习 Scala 的人无法立即掌握这些技巧。但是继续尝试和使用,以及查看测试和源代码,您将了解如何使用 Scala 创建强大的 DSL。

Normally, Scala symbols can be thought of as a unique identifier which will always be the same. Every symbol that is lexi-graphically identical refers to the exact same memory space. There's nothing else that's special about them from Scala's point of view.

However, Dispatch-Json pimps out symbols making them JSON property extractors. To see the code which is responsible for the pimping, check out the SymOp class and the rest of the JsonExtractor.scala code.

Let's write some code which solves the problem you are looking at and then analyze what's going on:

trait ExtUserProps extends UserProps with Js {
  val friends_count = 'friends_count ! num 
}
object ExtUser extends ExtUserProps with Js

val good_stuff = for {
  item <- http(Status("username").timeline)
  msg = Status.text(item)
  user = Status.user(item)
  screen_name = ExtUser.screen_name(user)
  friend_count = ExtUser.friends_count(user)
} yield (screen_name, msg, friend_count)

The first thing that we're doing is extending the UserProps trait in the Dispatch-Twitter module to give it a friends_count extractor and then defining a ExtUser object which we can use to get access to that extractor. Because the ExtUserProps extends UserProps, which also extends Js, we get the method sym_add_operators in scope which turns our symbol 'friends_count into a SymOp case class. We then call the ! method on that SymOp which we then pass the Extractor num to, which creates an Extractor that looks for a property "friends_count" on a JSON object and then parses it as a number before returning. Quite a bit going on there for such a small bit of code.

The next part of the program is just a for-comprehension that calls out to the Twitter timeline for a user and parses it into JsObjects which represent each status item, them we apply the Status.text extractor to pull out the status message. Then we do the same to pull out the user. We then pull the screen_name and friend_count out of the user JsObject and finally we yield a Tuple3 back with all of the properties we were looking for. We're then left with a List[Tuple3[String,String,BigDecimal]] which you could then iterate on to print out or do whatever with.

I hope that clears some things up. The Dispatch library is very expressive but can be a little tough to wrap your head around as it uses a lot of Scala tricks which someone just learning Scala won't get right away. But keep plugging around and playing with, as well as looking at the tests and source code, and you'll see how to create powerful DSL's using Scala.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文