这就是我得到的:
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModuleobject
AppStart extends App {
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
val json = """{"id":"AB","stuff":"whatever"}"""
val obj = mapper.readValue(json, classOf[TestClass])
println(obj.id.get) // prints AB !!!
}
case class TestClass(id: Option[Int] = None, stuff: Option[String] = None)
同时,这甚至没有建立:
val bad: Option[Int] = "AB"
这里显然有问题。我在项目中使用的版本:
scalaVersion := "2.11.6"
libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.7.3"