[英]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: 您的输入和解决方案存在一些问题:
Location
object and right after DigitalInputs
object. DigitalInputs
在Location
对象之后和DigitalInputs
对象之后。 You can validate your JSONs here . Reads
instance could be shortened significantly by using Json.reads
method. Json.reads
方法可以大大缩短您的Reads
实例。 Reads
instance for your myjson
class. myjson
类的Reads
实例。 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.