我试图将JSON反序列化为案例类。这是我的模特
case class Animal(
@JsonProperty("name") name : String,
@JsonProperty("weight") weight : Double,
@JsonProperty("unit-of-mass") unitOfMass: String
)
我的JSON文件
[
{
"name" : "Elephant",
"weight" : "13227.74",
"unit-of-mass" : "lbs"
},
{
"name" : "Panther",
"weight" : "31",
"unit-of-mass" : "kg"
}
]
并映射
val animals = sc.textFile("file:///...")
val mapper = new ObjectMapper
mapper.registerModule(DefaultScalaModule)
val json = animals.collect.mkString
val seq =
mapper.readValue(json, classOf[List[Animal]]) map {
case a @ Animal(n, w, u) => a
}
seq.foreach(println)
这是我遇到的错误
Exception in thread "main" java.lang.ClassCastException: scala.collection.immutable.Map$Map3 cannot be cast test.Animal
似乎杰克逊无法将JSON反序列化为bean,而不是制作地图地图。我该如何解决?