【发布时间】:2018-12-26 04:51:55
【问题描述】:
我需要使用 Retrofit2 解析的 JSON 响应**
{
"cod": "200",
"message": 0.0062,
"cnt": 4,
"list": [
{
"dt": 1531915200,
"main": {
"temp": 308,
"temp_min": 307.876,
"temp_max": 308,
"pressure": 1013.67,
"sea_level": 1028.96,
"grnd_level": 1013.67,
"humidity": 32,
"temp_kf": 0.13
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 1.96,
"deg": 90.0012
},
"sys": {
"pod": "d"
},
"dt_txt": "2018-07-18 12:00:00"
},
{
"dt": 1531926000,
"main": {
"temp": 309.55,
"temp_min": 309.452,
"temp_max": 309.55,
"pressure": 1012.51,
"sea_level": 1027.69,
"grnd_level": 1012.51,
"humidity": 30,
"temp_kf": 0.09
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 2.63,
"deg": 72.5014
},
"sys": {
"pod": "d"
},
"dt_txt": "2018-07-18 15:00:00"
},
{
"dt": 1531936800,
"main": {
"temp": 306.87,
"temp_min": 306.808,
"temp_max": 306.87,
"pressure": 1012.06,
"sea_level": 1027.49,
"grnd_level": 1012.06,
"humidity": 32,
"temp_kf": 0.06
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"clouds": {
"all": 0
},
"wind": {
"speed": 2.42,
"deg": 68.0013
},
"sys": {
"pod": "d"
},
"dt_txt": "2018-07-18 18:00:00"
},
{
"dt": 1531947600,
"main": {
"temp": 297.77,
"temp_min": 297.738,
"temp_max": 297.77,
"pressure": 1012.84,
"sea_level": 1028.22,
"grnd_level": 1012.84,
"humidity": 59,
"temp_kf": 0.03
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02n"
}
],
"clouds": {
"all": 24
},
"wind": {
"speed": 2.32,
"deg": 120.5
},
"sys": {
"pod": "n"
},
"dt_txt": "2018-07-18 21:00:00"
}
],
"city": {
"id": 2464470,
"name": "Tunis",
"coord": {
"lat": 36.8001,
"lon": 10.1848
},
"country": "TN",
"population": 693210
}
}
我的 Java 类“WeatherResponse”实例化了响应对象:
import com.google.gson.annotations.SerializedName;
public class WeatherResponse {
@SerializedName("cod")
private String cod;
@SerializedName("message")
private double message;
@SerializedName("cnt")
private double cnt;
@SerializedName("list")
private java.util.List<List> list =null;
public java.util.List<List> getList() {
return list;
}
}
我分配响应正文的代码行:
WeatherResponse wD = response.body();
我得到的回应:
【问题讨论】:
-
从图中,你的
response body不为空,点击body左边的箭头 -
我已经编辑了图像,你现在可以检查一下,问题是我只有一个正确的属性,即“cod”元素,我不明白为什么我的“list”元素是空
-
分享
List.java -
公共类列表 { private Main main;私有 java.util.List
天气 = null;私有字符串 dtTxt; public Main getMain() { return main; } public java.util.List getWeather() { 返回天气; } 公共字符串 getDtTxt() { 返回 dtTxt; } } -
这是用于显示“主要”天气温度的“主”类--> public class Main { private Double temp; public Double getTemp() { return temp; } }
标签: android json web-services parsing retrofit2