Sai
Sai

Reputation: 1117

How to parse JSON with lift-json in Scala?

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

Upvotes: 2

Views: 3145

Answers (1)

Markus1189
Markus1189

Reputation: 2869

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.

Upvotes: 1

Related Questions