我有JSON为
{
"city": {
"id": 2643743,
"name": "London",
"coord": {
"lon": -0.12574,
"lat": 51.50853
},
"country": "GB",
"population": 0
},
"cod": "200",
"message": 0.0155,
"cnt": 2,
"list": [
{
"dt": 1444993200,
"temp": {
"day": 13,
"min": 10.77,
"max": 13,
"night": 10.77,
"eve": 11.61,
"morn": 10.87
},
"pressure": 1029.46,
"humidity": 80,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"speed": 7.62,
"deg": 26,
"clouds": 92,
"rain": 0.21
},
{
"dt": 1445079600,
"temp": {
"day": 12.51,
"min": 10.81,
"max": 12.51,
"night": 11.04,
"eve": 11.34,
"morn": 10.81
},
"pressure": 1028.72,
"humidity": 79,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"speed": 6.71,
"deg": 17,
"clouds": 80,
"rain": 0.49
}
]
}
我想使用类
解析它public class Info {
private String cod;
private String message;
private Integer cnt;
private City city;
private java.util.List<List> conditionList = new ArrayList<List>();
public class City {
private Integer id;
private String name;
private String country;
private Coordinates coordinates;
//setters getters
public class Coordinates {
private Integer lon;
private Integer lat;
//setters getters
}
}
public class List {
private Integer dt;
private String pressure;
private Integer humidity;
private String speed;
private Integer deg;
private Integer clouds;
private Temprature temprature;
private java.util.List<Weather> weather = new ArrayList<Weather>();
//setters getters
public class Temprature {
private String day;
private String min;
private String max;
private String night;
private String eve;
private String morn;
//setters getters
}
public class Weather {
private Integer id;
private String main;
private String description;
private String icon;
//setters getters
}
}
}
当我尝试从List
类获取数据时
public static void main(String[] args) {
Util util = new Util();
InputStream source;
try {
source = util.retrieveStream();
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
Info response = gson.fromJson(reader, Info.class);
if (response != null) {
System.out.println("City is " + response.getCity().getName());// it gives Fine result
System.out.println("Pressure is " + response.getConditionList().get(0).getPressure());// Here comes ArrayIndexOutOfBoundsException
}
} catch (IOException e) {
e.printStackTrace();
}
}
StackTrace是
City is London
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at som.mirrors.lie.SimpleGSON.main(SimpleGSON.java:24)
当我调试时,我看到conditionList变量为空,如图所示
现在无法解析
"list": [
// contents above
]
我在Info课程中犯的任何错误?任何帮助是非常感谢! 提前谢谢。
答案 0 :(得分:2)
您需要在conditionList
POJO课程中将list
重命名为Info
。 JSON数据中没有名为conditionList
的元素(JSON密钥)。还要更新getter和setter。
private java.util.List<List> list = new ArrayList<List>();
public java.util.List<List> getList() {
return list;
}
public void setList(java.util.List<List> list) {
this.list = list;
}
<强>输出:强>
City is London
Pressure is 1029.46