简体   繁体   English

Scala中的Case类用于嵌套JSON

[英]Case Class in scala for nested JSON

SCALA 斯卡拉

My nested JSON appeared as given below. 我的嵌套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"}

I have written a case class as given below. 我已经编写了一个案例类,如下所示。

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,
)

but this is throwing error. 但这会引发错误。

can someone please help me. 有人可以帮帮我吗。

You need to create nested classes that match the nested JSON, something like this: 您需要创建与嵌套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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM