如何在Scala中使用lift-json解析JSON? [英] How to parse JSON with lift-json in Scala?
问题描述
当我尝试解析 json 对象时,出现以下错误.
When I am trying parse the json object I am getting the below error.
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"到我的构建路径,我收到以下错误:
I have added "lift-json_2.9.0-1-2.4.jar " to my build path and I am getting following error:
找不到参数格式的隐含值:net.liftweb.json.Formats
could not find implicit value for parameter formats: net.liftweb.json.Formats
方法提取的参数不足:(隐式格式:net.liftweb.json.Formats,隐式 mf:scala.reflect.Manifest[MailServer])MailServer.未指定值参数格式,mf
not enough arguments for method extract: (implicit formats: net.liftweb.json.Formats, implicit mf: scala.reflect.Manifest[MailServer])MailServer. Unspecified value parameters formats, mf
推荐答案
您的示例适用于 Scala 2.11.7
和 lift-json-2.6.2
.你用的是什么版本的scala?从您上面给出的 jar
的名称来看,您应该使用 scala 2.9.*
,它已经很老了.如果您不在 Scala 2.9.*
上,我想这是因为 Scala 版本之间的二进制不兼容.如果您使用 sbt
,请尝试以下 build.sbt
:
Your example works for me on scala 2.11.7
and lift-json-2.6.2
. What version of scala are you using? From the name of the jar
you gave above you should be using scala 2.9.*
which is pretty old. If you're not on scala 2.9.*
I guess it is because of binary incompatibilities between the Scala versions. If you are using sbt
, try the following as build.sbt
:
name := "<name of your project>"
scalaVersion := "2.11.7"
libraryDependencies += "net.liftweb" %% "lift-json" % "2.6.2"
然后您可以删除旧的 jar
文件,因为 sbt
会为您处理.
You can then remove the old jar
file because sbt
takes care of that for you.
这篇关于如何在Scala中使用lift-json解析JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!