简体   繁体   中英

ArrayIndexOutOfBoundsException when parsing JSON using GSON

I have JSON as

{
  "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

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

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

条件列表为空

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. There is not element (JSON key) named conditionList in the JSON data. Also update the getter and setter as well.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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