我如何获得天气响应的json响应

时间:2019-06-01 11:24:00

标签: json kotlin

我正在使用天气地图api来获取当前天气,但确实获得了温度,但我在获取描述方面存在问题

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
  @SerializedName("humidity")
  var humidity: Float = 0.0f
  @SerializedName("pressure")
  var pressure: Float = 0.0f
  @SerializedName("temp_min")
  var temp_min: Float = 0.0f
  @SerializedName("temp_max")
  var temp_max: Float = 0.0f
}

class Sys {
  @SerializedName("country")
  var country: String? = null
}

这是网址的响应

  {"coord":{"lon":139,"lat":35},
  "sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
  "weather":[{"id":804,"main":"clouds","description":"overcast 
   clouds","icon":"04n"}],
  "main": 
  {"temp":289.5,"humidity":89,"pressure":1013,"temp_min"
  :287.04,"temp_max" :292.04},
  "wind":{"speed":7.31,"deg":187.002},
  "rain":{"3h":0},
 "clouds":{"all":92},
 "dt":1369824698,
 "id":1851632,
"name":"Shuzenji",
 "cod":200}

1 个答案:

答案 0 :(得分:0)

编辑:对不起,这是一个python解决方案。

如果将json字符串转换为字典,则可以尝试以下操作:

weather_dict = json.loads(json_str)
print(weather_dict['weather'][0]['description'])

输出:

overcast clouds