有一个http响应,它是一个JSON字符串
{"id":"12345","dob":"01\/01\/1991","first_name":"Joe","gender":"male"}
它需要实例化到这个类中。
case class UserRow(id: Long, firstName: String, lastName: String, dob: Long, gender: String)
我试着将JSON解析成一个映射
val result = parseFull(response)
println(result)
输出
Some(Map(dob -> 01/01/1991, id -> 12345, first_name -> Joe, gender -> male))
试着
map.get("id").toString().toLong //Throws a NumberFormatException
道布应该转换为millis (EPOC)型。我们很感谢你的帮助
发布于 2014-10-13 05:54:24
基本答案您可以使用https://github.com/json4s/json4s或其他JSON。并使用序列化器。
我需要调整JSON和case类的某些部分。
一些代码示例
import org.json4s._
import org.json4s.native.Serialization
implicit val formats = Serialization.formats(NoTypeHints)
val jsonExample = """{"id":12345,"firstName":"Joe","gender":"male"}"""
case class UserRow(id: Long, firstName: String, lastName: Option[String], dob: Option[Long], gender: String)
使用实例
scala> Serialization.read[UserRow](jsonExample)
res5: UserRow = UserRow(12345,Joe,None,None,male)
扩展应答
提供的JSON导致了几个问题,这些问题只能使用手工编写的反序列化器https://github.com/json4s/json4s#serialization来解决。
https://stackoverflow.com/questions/26337837
复制相似问题