【问题标题】:ArrayIndexOutOfBoundsException when parsing JSON using GSON使用 GSON 解析 JSON 时出现 ArrayIndexOutOfBoundsException
【发布时间】:2015-10-16 08:59:56
【问题描述】:

我有 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 课上犯了什么错误?任何帮助都非常感谢! 提前致谢。

【问题讨论】:

  • 您的列表中没有任何列表...您有 JSON 对象...但是您已经定义了 ArrayList
  • 你应该先试试看 if(response.getConditionList().get(0) !=null) 在调用 .getPressure() 之前,你不知道你的getConditionList()

标签: java json gson


【解决方案1】:

您需要在Info POJO 类中将conditionList 重命名为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

【讨论】:

  • @TaharBakir 因为默认情况下 Gson API 将变量名称映射到 JSON 中的 key
猜你喜欢
  • 2011-12-03
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多