def get(cityName: String): Future[JsResult[Forecast]] = {
val tempUrl = HOST + s"?q=$cityName&appid=$appid"
ws.url(tempUrl).get().map { response =>
(response.json).validate[Forecast]
}
}
Код модели Forecast такой
case class Forecast(
weatherType: String,
description: String,
temperature: Double,
humidity: Int,
windSpeed: Double,
clouds: Int
)
object Forecast {
implicit val forecastReads: Reads[Forecast] = (
(JsPath \ "weather" \ 0 \ "main").read[String] and
(JsPath \ "weather" \ 0 \ "description").read[String] and
(JsPath \ "main" \ "temp").read[Double] and
(JsPath \ "main" \ "humidity").read[Int] and
(JsPath \ "wind" \ "speed").read[Double] and
(JsPath \ "clouds" \ "all").read[Int]
) (Forecast.apply _)
implicit val forecastImplicitWrites = Json.writes[Forecast]
}
Как получить обьект типа Forecast в контроллере и вывести на странице в json format ?
scastie.scala-lang.org
Обсуждают сегодня