为嵌套的json处理播放json

时间:2017-03-13 10:01:08

标签: json scala playframework

我们有一个像下面这样的json

{
    "-KULpL4Qrzt4z8Go": {
        "dateTime": 1476778076353,
        "partyName": "AMBA",
        "partyId": "A101",
        "points": { 
            "-KULr3tag86GlJSZp": {
                "lat": 71.1426377,
                "lon": 29.0803357,
                "dtime": 1476778091998
            },
            "-KULr3v9zJA4NqLOf": {
                "lat": 91.1426377,
                "lon": 26.0803357,
                "dtime": 
            }
        } 
    }
  }

我使用play json(对于Scala 2.10.6)并希望将数据输入模型

case class JsonPlay(dateTime:String,partyName:String,partyId:String) 
case class PointsPlay(lat:Double, lon:Double, dtime:Option[BigInt])

但是我的第一个Read本身就抛出异常,说无法找到\ dateTime等。

implicit val baseReads: Reads[JsonPlay] = (
          (JsPath \ "dateTime").read[String] and
          (JsPath \ "partyName").read[String] and
          (JsPath \ "partyId").read[String] and
    )(JsonPlay.apply _)

感谢任何指针/帮助来完成这项工作。感谢。

-Venkiram

1 个答案:

答案 0 :(得分:1)

在这种情况下,

dateTime很长。它应该是:

case class JsonPlay(dateTime: Long, partyName: String, partyId: String) 

 implicit val baseReads: Reads[JsonPlay] = (
          (JsPath \ "dateTime").read[Long] and
          (JsPath \ "partyName").read[String] and
          (JsPath \ "partyId").read[String] and
    )(JsonPlay.apply _)