簡體   English   中英

使用GSON解析JSON時出現ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException when parsing JSON using GSON

我有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
      ]

我在信息課上犯了什么錯誤? 任何幫助,高度贊賞! 提前致謝。

您需要重命名conditionList以在Info POJO類中list 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM