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.
Sieht gut aus, ich habe jetzt ein 'nicht Symbol und' mit den und Operator – jag
Ich glaube, Sie lösen hier fehlt ein Import: 'import play.api.libs.functional.syntax._' –
Ja, das war das Problem. Danke Pawel – jag