Json4s无法在scala中反序列化
Json4s unable to desrialize in scala
我需要 json 使用 Json4s 在 Scala 中进行反序列化的帮助。当我尝试反序列化 json
时出现以下错误
scala.collection.immutable.Map$Map1 cannot be cast to
LocationServiceTest$$anonfun$LocationList
java.lang.ClassCastException: scala.collection.immutable.Map$Map1 cannot be cast to
LocationServiceTest$$anonfun$LocationList
at LocationServiceTest$$anonfun.apply$mcV$sp(LocationServiceTest.scala:34)
at LocationServiceTest$$anonfun.apply(LocationServiceTest.scala:16)
at LocationServiceTest$$anonfun.apply(LocationServiceTest.scala:16)
我的Json如下:
{
"locations" : [
{
"country": "YY",
"placeName": "YY",
"latitude": "YY",
"longitude": "YY"
},
{
"country": "XX",
"placeName": "XX",
"latitude": "XX",
"longitude": "XX"
},
我的代码:
case class Location(country: String, placeName: String, latitude: Double, longitude: Double)
val locations = parse(scala.io.Source.fromFile("input.json").mkString)
println(locations.values.asInstanceOf[LocationList])
使用 extract(format, manifest) 也失败了。有人可以帮忙吗。
parse
函数创建 JValue
(JSON Scala 类型的表示,如 Map
、Array
和原始类型)。该类型与某些自定义类型没有任何关系,例如 LocationList
。为此,您需要使用 json.extract
。见 https://github.com/json4s/json4s Extracting values:
Case classes can be used to extract values from parsed JSON
json.extract[Person]
代码如下:
object Test extends App {
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats =DefaultFormats
val str = scala.io.Source.fromFile("input.json").mkString
case class Location(country: String, placeName: String, latitude: String, longitude: String)
val dataObj= parse(str)
val locObj = (dataObj \ "locations").extract[Seq[Location]]
println(locObj)
}
输出:
List(Location(YY,YY,YY,YY), Location(XX,XX,XX,XX))
请注意,如果 class,纬度和经度是字符串,因为在 json 中,您将它们声明为字符串。
如果有帮助请告诉我!!
我需要 json 使用 Json4s 在 Scala 中进行反序列化的帮助。当我尝试反序列化 json
时出现以下错误scala.collection.immutable.Map$Map1 cannot be cast to
LocationServiceTest$$anonfun$LocationList
java.lang.ClassCastException: scala.collection.immutable.Map$Map1 cannot be cast to
LocationServiceTest$$anonfun$LocationList
at LocationServiceTest$$anonfun.apply$mcV$sp(LocationServiceTest.scala:34)
at LocationServiceTest$$anonfun.apply(LocationServiceTest.scala:16)
at LocationServiceTest$$anonfun.apply(LocationServiceTest.scala:16)
我的Json如下:
{
"locations" : [
{
"country": "YY",
"placeName": "YY",
"latitude": "YY",
"longitude": "YY"
},
{
"country": "XX",
"placeName": "XX",
"latitude": "XX",
"longitude": "XX"
},
我的代码:
case class Location(country: String, placeName: String, latitude: Double, longitude: Double)
val locations = parse(scala.io.Source.fromFile("input.json").mkString)
println(locations.values.asInstanceOf[LocationList])
使用 extract(format, manifest) 也失败了。有人可以帮忙吗。
parse
函数创建 JValue
(JSON Scala 类型的表示,如 Map
、Array
和原始类型)。该类型与某些自定义类型没有任何关系,例如 LocationList
。为此,您需要使用 json.extract
。见 https://github.com/json4s/json4s Extracting values:
Case classes can be used to extract values from parsed JSON
json.extract[Person]
代码如下:
object Test extends App {
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats =DefaultFormats
val str = scala.io.Source.fromFile("input.json").mkString
case class Location(country: String, placeName: String, latitude: String, longitude: String)
val dataObj= parse(str)
val locObj = (dataObj \ "locations").extract[Seq[Location]]
println(locObj)
}
输出:
List(Location(YY,YY,YY,YY), Location(XX,XX,XX,XX))
请注意,如果 class,纬度和经度是字符串,因为在 json 中,您将它们声明为字符串。
如果有帮助请告诉我!!