我收到了以下json到我的代码中,因为我是scala的新手,我很难解决问题
这是json:
{"id":1486363,"doc":{"v": "1", "x": "1", "y": "1", "z": "1", "lux": "0", "rssi": "440", "alarm": "0", "magnet": "3", "agesent": "540227616", "datetime": "2017/11/24 15:38:22.402902", "movement": "65535", "readerId": 75, "sensorId": "3217003", "uniqueName": "hive", "locationCounter": "0", "locationSensorId": "0"},"created_at":"2017-11-18T10:00:00","sensorId":null,"readerId":null}
我有这个案例类,可以将json强制转换为
case class SensorRead(id: String, doc: JObject, created_at: Timestamp)
这是导致问题的代码
val json = parse(not.getParameter) // json
val prod = json.noNulls.extract[SensorRead]
这就是错误
No usable value for id
Did not find value which can be converted into java.lang.String
org.json4s.package$MappingException: No usable value for id
Did not find value which can be converted into java.lang.String
修改
下面是时间戳的格式化程序 - 我也使用json4s库 - “org.json4s”%%“json4s-native”%“3.5.3”,
implicit val formats = new DefaultFormats {
override def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
}
答案 0 :(得分:0)
您将id
作为Int获取,请尝试以下操作:(您也可能需要编写createdAt
)
case class SensorRead(id: Int, doc: JObject, created_at: Timestamp)