我如何解析json天气响应

时间:2019-06-01 13:20:29

标签: json kotlin retrofit

这是我对天气地图api的回复

      {
            "message": "accurate",
            "cod": "200",
"count": 3,
"list": [
    {
        "id": 2641549,
        "name": "Newtonhill",
        "coord": {
            "lat": 57.0333,
            "lon": -2.15
        },
        "main": {
            "temp": 275.15,
            "pressure": 1010,
            "humidity": 93,
            "temp_min": 275.15,
            "temp_max": 275.15
        },
        "dt": 1521204600,
        "wind": {
            "speed": 9.3,
            "deg": 120,
            "gust": 18
        },
        "sys": {
            "country": ""
        },
        "rain": null,
        "snow": null,
        "clouds": {
            "all": 75
        },
        "weather": [
            {
                "id": 311,
                "main": "Drizzle",
                "description": "rain and drizzle",
                "icon": "09d"
            }
        ]
    }

我该如何获得描述,我确实通过使用可序列化对象的改造获得了温度,但我却无法获得天气描述 我这样做是为了获得温度和国家        class WeatherResponse {

     @SerializedName("sys")
     var sys: Sys? = null

    @SerializedName("main")
    var main: Main? = null

    @SerializedName("weather")
   var weather: Weather? = null
 }


class Main {
   @SerializedName("temp")
   var temp: Float = 0.0f

   }

和我我的主类我使用回调            有趣的getCurrentData(){

    val retrofit = Retrofit.Builder()
        .baseUrl(BaseUrl)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
    val service = retrofit.create(WeatherService::class.java)
    val call = service.getCurrentWeatherData(lat, lon, AppId)

    call.enqueue(object : Callback<WeatherResponse> {
        override fun onResponse(call: Call<WeatherResponse>, response: Response<WeatherResponse>) {
            if (response.code() == 200) {
                val weatherResponse = response.body()!!
                var temp = (weatherResponse.main!!.temp - 273).toString().substring(0,3) + " ºC"
                tmp.text=temp

            }
        }

        override fun onFailure(call: Call<WeatherResponse>, t: Throwable) {

        }
    })
}

1 个答案:

答案 0 :(得分:0)

这是您的JSON的POJO类。现在可以轻松对其进行解析了。

WeatherResponse.kt

data class WeatherResponse(
    @SerializedName("cod")
    val cod: String? = null,
    @SerializedName("count")
    val count: Int? = null,
    @SerializedName("list")
    val list: List<X?>? = null,
    @SerializedName("message")
    val message: String? = null
) {
    data class X(
        @SerializedName("clouds")
        val clouds: Clouds? = null,
        @SerializedName("coord")
        val coord: Coord? = null,
        @SerializedName("dt")
        val dt: Int? = null,
        @SerializedName("id")
        val id: Int? = null,
        @SerializedName("main")
        val main: Main? = null,
        @SerializedName("name")
        val name: String? = null,
        @SerializedName("rain")
        val rain: Any? = null,
        @SerializedName("snow")
        val snow: Any? = null,
        @SerializedName("sys")
        val sys: Sys? = null,
        @SerializedName("weather")
        val weather: List<Weather?>? = null,
        @SerializedName("wind")
        val wind: Wind? = null
    )

    data class Clouds(
        @SerializedName("all")
        val all: Int?
    )

    data class Coord(
        @SerializedName("lat")
        val lat: Double? = null,
        @SerializedName("lon")
        val lon: Double? = null
    )

    data class Main(
        @SerializedName("humidity")
        val humidity: Int? = null,
        @SerializedName("pressure")
        val pressure: Int? = null,
        @SerializedName("temp")
        val temp: Double? = null,
        @SerializedName("temp_max")
        val tempMax: Double? = null,
        @SerializedName("temp_min")
        val tempMin: Double? = null
    )

    data class Sys(
        @SerializedName("country")
        val country: String?
    )

    data class Weather(
        @SerializedName("description")
        val description: String? = null,
        @SerializedName("icon")
        val icon: String? = null,
        @SerializedName("id")
        val id: Int? = null,
        @SerializedName("main")
        val main: String? = null
    )

    data class Wind(
        @SerializedName("deg")
        val deg: Int? = null,
        @SerializedName("gust")
        val gust: Int? = null,
        @SerializedName("speed")
        val speed: Double? = null
    )

}

您可以通过使用获取天气描述

var description = weatherResponse.list?.get(0)?.weather?.get(0)?.description