Scala中的Case类用于嵌套JSON

时间:2019-07-02 04:23:05

标签: scala spray-json

SCALA

我的嵌套JSON如下所示。

{"event":"movie","on":"bollywood","reldate":"2019-06-01T18:20:00.560Z","data":{"id":"4965-81dc-4ef","location":{"ratioin":52.48077,"ratioout":13.42499,"reldate":"2019-06-01T18:20:00.560Z"}},"mov_id":"org-id"}

我写了一个案例类,如下所示。

import org.apache.spark.sql.SparkSession
import spray.json.DefaultJsonProtocol
import spray.json._
import spark.implicits._


case class Activity(
    event: string,
    on :string,
    reldate :string,
    data: array[1],
    location: array[2],
    mov: string,
)

但这会引发错误。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

您需要创建与嵌套JSON相匹配的嵌套类,如下所示:

case class Activity(
  event: String,
  on: String,
  reldate: String,
  data: ActivityData,
  mov_id: String,
)

case class ActivityData(
  id: String,
  location: ActivityLocation
)

case class ActivityLocation(
  ratioin: Double,
  ratiouut: Double,
  reldate: String
)