播放框架JSON解析错误
我有以下数据类型:播放框架JSON解析错误,json,playframework,Json,Playframework,我有以下数据类型: case class DataPoints(fromSource: String, dataPoints: Seq[DataPoint]) object DataPoints { implicit val tsFormat = Json.format[DataPoint] implicit val tsDataPointsFormat: Format[DataPoints] = ((__ \ "fromSource").format[String] ~
case class DataPoints(fromSource: String, dataPoints: Seq[DataPoint])
object DataPoints {
implicit val tsFormat = Json.format[DataPoint]
implicit val tsDataPointsFormat: Format[DataPoints] =
((__ \ "fromSource").format[String] ~
(__ \ "dataPoints").format[Seq[DataPoint]]
)(DataPoints.apply, unlift(DataPoints.unapply))
}
在我使用此Json转换的地方:
private def toDataPointSeq(consumerRecords: Seq[ConsumerRecord[String, String]]) = {
import com.model.DataPoints._
consumerRecords.map {
case record =>
logger.info(s"value obtained is ${record.value()}")
val jsonIs = Json.toJson(record.value()).validate[DataPoints].asOpt
logger.info(s"json obtained is $jsonIs") // prints None
jsonIs
}.collect {
case someDataPts if someDataPts.isDefined => someDataPts.get.dataPoints
}
}
即使line record.value()打印以下内容,我得到的json也会返回为None:
{
"fromSource":"kafkaProducer",
"dataPoints":[
{
"metric":"dataPoint.Active",
"timestamp":1453830484838,
"value":0.0,
"tags":{
"Id":"100"
}
}
]
}
验证Json后,我为什么看不到任何问题?我找到了问题的原因和所在。事实上,我从Kafka获取的值是Json,但Json包含额外的转义字符,如下所示:
"{\"fromSource\":\"kafkaProducer\",\"dataPoints\":
我不得不做额外的解析来取消转义字符