How to parse JSON with lift-json in Scala?

这一生的挚爱 提交于 2020-01-15 15:46:15

问题


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)
  }
}

I have added "lift-json_2.9.0-1-2.4.jar " to my build path and I am getting following error:

could not find implicit value for parameter formats: net.liftweb.json.Formats

not enough arguments for method extract: (implicit formats: net.liftweb.json.Formats, implicit mf: scala.reflect.Manifest[MailServer])MailServer. Unspecified value parameters formats, mf


回答1:


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"

You can then remove the old jar file because sbt takes care of that for you.



来源:https://stackoverflow.com/questions/34649989/how-to-parse-json-with-lift-json-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!