如何使用 Lift 将 x-www-url 编码的字符串解析为 Map[String, String]?

发布于 2024-11-18 02:14:19 字数 313 浏览 0 评论 0原文

获取了以下形式的字符串

TOKEN=EC%2d454178600K772032D&TIMESTAMP=2011%2d06%2d29T13%3a10%3a58Z&CORRELATIONID=cbd56e97cad38&ACK=Success&VERSION=64&BUILD=1936884

从 Lift 中,我从 HTTP 请求的响应中

。尽管它可能非常简单,但我找不到将其解析为漂亮的 Map[String, String] 的 Lift 函数。有什么帮助吗?

From Lift, I'm getting a string of the form

TOKEN=EC%2d454178600K772032D&TIMESTAMP=2011%2d06%2d29T13%3a10%3a58Z&CORRELATIONID=cbd56e97cad38&ACK=Success&VERSION=64&BUILD=1936884

from the response of an HTTP request.

Although it's probably ultra-trivial, I can't find the Lift function that parses this into a nice Map[String, String]. Any help?

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

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

发布评论

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

评论(4

苏佲洛 2024-11-25 02:14:19

来自 Lift 的 Req.scala:

// calculate the query parameters
lazy val queryStringParam:  (List[String], Map[String, List[String]]) = {
  val params: List[(String, String)] =
    for {
      queryString <- request.queryString.toList
      nameVal <- queryString.split("&").toList.map(_.trim).filter(_.length > 0)
      (name, value) <- nameVal.split("=").toList match {
        case Nil => Empty
        case n :: v :: _ => Full((urlDecode(n), urlDecode(v)))
        case n :: _ => Full((urlDecode(n), ""))
      }} yield (name, value)

        val names: List[String] = params.map(_._1).distinct
  val nvp: Map[String, List[String]] = params.foldLeft(Map[String, List[String]]()) {
    case (map, (name, value)) => map + (name -> (map.getOrElse(name, Nil) ::: List(value)))
  }

  (names, nvp)
}

From Lift's Req.scala:

// calculate the query parameters
lazy val queryStringParam:  (List[String], Map[String, List[String]]) = {
  val params: List[(String, String)] =
    for {
      queryString <- request.queryString.toList
      nameVal <- queryString.split("&").toList.map(_.trim).filter(_.length > 0)
      (name, value) <- nameVal.split("=").toList match {
        case Nil => Empty
        case n :: v :: _ => Full((urlDecode(n), urlDecode(v)))
        case n :: _ => Full((urlDecode(n), ""))
      }} yield (name, value)

        val names: List[String] = params.map(_._1).distinct
  val nvp: Map[String, List[String]] = params.foldLeft(Map[String, List[String]]()) {
    case (map, (name, value)) => map + (name -> (map.getOrElse(name, Nil) ::: List(value)))
  }

  (names, nvp)
}
原谅我要高飞 2024-11-25 02:14:19

我还没有看到任何 Lift 的实现。您可以通过以下方式实现此目的:

val input = "TOKEN=EC%2d454178600K772032D&TIMESTAMP=2011%2d06%2d29T13%3a10%3a58Z&CORRELATIONID=cbd56e97cad38&ACK=Success&VERSION=64&BUILD=1936884"
val res = input.split('&') map { str =>
    val pair = str.split('=')
    (pair(0) -> pair(1))
} toMap

注意:它假设您有一个格式良好的字符串。在您的代码中,您可能应该检查字符串是否正确。

I haven't seen any Lift's implementation for that. You can achieve this with something like this:

val input = "TOKEN=EC%2d454178600K772032D&TIMESTAMP=2011%2d06%2d29T13%3a10%3a58Z&CORRELATIONID=cbd56e97cad38&ACK=Success&VERSION=64&BUILD=1936884"
val res = input.split('&') map { str =>
    val pair = str.split('=')
    (pair(0) -> pair(1))
} toMap

note: it assumes, that you have a well-formed string. In your code you should probably check if the string is ok.

醉态萌生 2024-11-25 02:14:19

我整理了一个小型 Scala 库来帮助完成此任务: https://github.com/theon/scala-uri

您可以解析 uri 并将参数获取到 Map[String,List[String]] 中,如下所示:

val uri = parseUri("http://example.com?one=1&two=2").query.params

它还有一个 DSL,用于使用查询字符串构建 URL:

val uri = "http://example.com" ? ("one" -> 1) & ("two" -> 2)

I put together a small Scala library to help do this: https://github.com/theon/scala-uri

You can parse a uri and get the parameters into a Map[String,List[String]] like so:

val uri = parseUri("http://example.com?one=1&two=2").query.params

It also has a DSL for building URLs with query strings:

val uri = "http://example.com" ? ("one" -> 1) & ("two" -> 2)
裂开嘴轻声笑有多痛 2024-11-25 02:14:19
scala> val queryParams = "a=4&b=5"
scala> queryParams.split("&").toList.map(_.trim).filter(_.length > 0).flatMap(x => {
    val s = x.split('=')
    Map[String, String](s(0) -> s(1))
}).toMap[String, String]

res0: scala.collection.immutable.Map[String,String] = Map(a -> 4, b -> 5)
scala> val queryParams = "a=4&b=5"
scala> queryParams.split("&").toList.map(_.trim).filter(_.length > 0).flatMap(x => {
    val s = x.split('=')
    Map[String, String](s(0) -> s(1))
}).toMap[String, String]

res0: scala.collection.immutable.Map[String,String] = Map(a -> 4, b -> 5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文