【问题标题】:How to parse JSON with lift-json in Scala?如何在 Scala 中使用 lift-json 解析 JSON?
【发布时间】:2016-04-11 12:55:52
【问题描述】:

当我尝试解析 json 对象时,出现以下错误。

import net.liftweb.json._

object SarahEmailPluginConfigTest {
  implicit val formats = DefaultFormats

  case class Mailserver(url: String, username: String, password: String)

  val json = parse("""{"url": "imap.yahoo.com", "username": "myusername", "password": "mypassword" }""")

  def main(args: Array[String]) {
    val m = json.extract[Mailserver]
    println(m.url)
    println(m.username)
    println(m.password)
  }
}

我已将“lift-json_2.9.0-1-2.4.jar”添加到我的构建路径中,但出现以下错误:

找不到参数格式的隐含值:net.liftweb.json.Formats

方法提取的参数不足:(隐式格式:net.liftweb.json.Formats,隐式 mf:scala.reflect.Manifest[MailServer])MailServer。未指定值参数格式,mf

【问题讨论】:

  • 不确定这个特定的库,但听起来你需要注册你的案例类。 implicit val formats = DefaultFormats 对于一组基本类型来说可能就足够了,但是您正在添加自己的类型,并且可能需要为其提供隐式格式。

标签: scala scalability scala-collections scalaz


【解决方案1】:

您的示例适用于 scala 2.11.7lift-json-2.6.2。你用的是什么版本的scala?从您上面给出的jar 的名称来看,您应该使用相当古老的scala 2.9.*。如果你不在 scala 2.9.* 我猜这是因为 Scala 版本之间的二进制不兼容。如果您使用sbt,请尝试以下build.sbt

name := "<name of your project>"

scalaVersion := "2.11.7"

libraryDependencies += "net.liftweb" %% "lift-json" % "2.6.2"

然后您可以删除旧的 jar 文件,因为 sbt 会为您处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多