简体   繁体   English

Scala读取JSON文件

[英]Scala Read JSON file

i want to extract a json file in Scala like this: 我想像这样在Scala中提取一个json文件:

val json: JsValue = Json.parse("""
{
"Received":"2015-12-29T00:00:00.000Z",
"Created":"2015-12-29T00:00:00.000Z",
"Location":{
  "Created":"2015-12-29T00:00:00.000Z",
  "Coordinate":{
      "Latitude":45.607807,
      "Longitude":-5.712018},
      },
"Infos":[],
"DigitalInputs":[{
        "TypeId":145,
        "Value":false,
        "Index":23
        }],
      }
                               """)

and this is my Scala code: 这是我的Scala代码:

import org.apache.flink.api.scala._
import play.api.libs.json._

case class DInputs(
                  TypeId: Option[Int],
                  Value: Option[Boolean],
                  Index: Option[Int]
                  )

case class myjson (
                  Received: String,
                  Created: String,
                  Location: Option[String],
                  Infos: Option[String],
                  DigitalInputs: Option[List[DInputs]],
                  )

implicit val DInputsRead: Reads[Option[DInputs]] = (
    (__ \ "TypeId").readNullable[Int] andThen
    (__ \ "Value").readNullable[Boolean] andThen
    (__ \ "Index").readNullable[Int]
  )(DInputs.apply _)

case Some(json.DInputsRead) => println(json.DInputsRead)

The error in my code: Expression of type Reads[Option[Int]] doesn´t conform to expected type Reads[Option[DInputs]] 我的代码中的错误: Expression of type Reads[Option[Int]] doesn´t conform to expected type Reads[Option[DInputs]]

i´m newbie and don´t understand where is the problem, and i don´t know if this is the best way to read a json file, so any help is appreciate.Thank you. 我是新手,不知道问题出在哪里,我也不知道这是否是读取json文件的最佳方法,所以对您有所帮助。谢谢。

There are a few issues with your input and your solution: 您的输入和解决方案存在一些问题:

  1. Your input JSON is invalid: it contains excessive commas in two places: right after Location object and right after DigitalInputs object. 您输入的JSON无效:它在两个位置包含过多的逗号: DigitalInputsLocation对象之后和DigitalInputs对象之后。 You can validate your JSONs here . 您可以在此处验证JSON。
  2. Your Reads instance could be shortened significantly by using Json.reads method. 使用Json.reads方法可以大大缩短您的Reads实例。
  3. You're missing a Reads instance for your myjson class. 您缺少myjson类的Reads实例。
  4. You're missing a case class for a Location object. 您缺少Location对象的案例类。

Here is a complete example of parsing your input JSON to a regular Scala case class: 这是将输入JSON解析为常规Scala案例类的完整示例:

  import play.api.libs.json.Json

  case class MyJson(Received: String,
                    Created: String,
                    Location: Option[Location],
                    Infos: Option[List[String]],
                    DigitalInputs: Option[List[DigitalInputs]])

  case class Location(Created: String,
                      Coordinate: Coordinate)

  case class Coordinate(Latitude: Double,
                        Longitude: Double)

  case class DigitalInputs(TypeId: Option[Int],
                           Value: Option[Boolean],
                           Index: Option[Int])

  implicit val digitalInputsReads = Json.reads[DigitalInputs]
  implicit val coordinateReads = Json.reads[Coordinate]
  implicit val locationReads = Json.reads[Location]
  implicit val myJsonReads = Json.reads[MyJson]

  val inputJson = Json.parse(
    """
      |{
      |  "Received":"2015-12-29T00:00:00.000Z",
      |  "Created":"2015-12-29T00:00:00.000Z",
      |  "Location":{
      |    "Created":"2015-12-29T00:00:00.000Z",
      |    "Coordinate":{
      |      "Latitude":45.607807,
      |      "Longitude":-5.712018
      |    }
      |  },
      |  "Infos":[
      |
      |  ],
      |  "DigitalInputs":[
      |    {
      |      "TypeId":145,
      |      "Value":false,
      |      "Index":23
      |    }
      |  ]
      |}
    """.stripMargin
  )

  val myJsonInstance: MyJson = inputJson.as[MyJson]
  val longitude: Option[Double] = myJsonInstance.Location.map(_.Coordinate.Longitude) // Some(-5.712018)
  val typeId: Option[Int] = myJsonInstance.DigitalInputs.flatMap(_.headOption.flatMap(_.TypeId)) // Some(145)

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

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