2016-06-01 14 views
0

Ich möchte wie diese eine JSON-Datei in Scala extrahieren:Scala lesen JSON-Datei

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 
     }], 
     } 
           """) 

und das ist mein Scala Code:

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) 

Der Fehler in meinem Code: Expression of type Reads[Option[Int]] doesn´t conform to expected type Reads[Option[DInputs]]

ich bin Neuling und verstehe nicht, wo das Problem ist, und ich weiß nicht, ob dies der beste Weg ist, eine JSON-Datei zu lesen, so dass jede Hilfe zu schätzen ist. Danke.

Antwort

0

Es gibt ein paar Probleme mit dem Eingang und Ihre Lösung:

  1. Ihre Eingabe JSON ist ungültig: direkt nach Location Objekt und direkt nach DigitalInputs Objekt: es übermäßige Kommas an zwei Stellen enthält. Sie können Ihre JSONs here validieren.
  2. Ihre Reads Instanz konnte durch Verwendung der Methode erheblich verkürzt werden.
  3. Sie vermissen eine Reads Instanz für Ihre myjson Klasse.
  4. Sie vermissen eine Fallklasse für ein Objekt Location.

Hier ist ein vollständiges Beispiel für das Parsen Ihrer Eingabe JSON zu einer regulären Scala Fall Klasse:

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) 
+0

Sieht gut aus, ich habe jetzt ein 'nicht Symbol und' mit den und Operator – jag

+0

Ich glaube, Sie lösen hier fehlt ein Import: 'import play.api.libs.functional.syntax._' –

+0

Ja, das war das Problem. Danke Pawel – jag