简体   繁体   English

使用GSON解析JSON时出现ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException when parsing JSON using GSON

I have JSON as 我有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
    }
  ]
}

I want to parse this using the class as 我想使用类解析为

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

        }
    }

}

when I try to get get data from List class as 当我尝试从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 is 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)

When I debug I see conditionList variable empty as shown 当我调试时,我看到conditionList变量为空,如图所示

条件列表为空

Now Don't be able to parse 现在无法解析

"list": [
        // contents above
      ]

any mistakes am I making in Info class? 我在信息课上犯了什么错误? any help is Highly appreciated! 任何帮助,高度赞赏! Thanks in advance. 提前致谢。

You need to rename the conditionList to list in the Info POJO class. 您需要重命名conditionList以在Info POJO类中list There is not element (JSON key) named conditionList in the JSON data. JSON数据中没有名为conditionList元素(JSON密钥)。 Also update the getter and setter as well. 还要更新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;
}

Output: 输出:

City is London
Pressure is 1029.46

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM