简体   繁体   中英

Converting with GSON

I am currently working on a kind of weather app. Therefor I have to parse A JSON Object. I use GSON for that. I always get an error.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 224 path $.list[0].weather

The JSON Object looks something like this:

{"city":{"id":1851632,"name":"Shuzenji"},
 "coord":{"lon":138.933334,"lat":34.966671},
 "country":"JP",
 "cod":"200",
 "message":0.0045,
 "cnt":38,
 "list":[{
    "dt":1406106000,
    "main":{
        "temp":298.77,
        "temp_min":298.77,
        "temp_max":298.774,
        "pressure":1005.93,
        "sea_level":1018.18,
        "grnd_level":1005.93,
        "humidity":87,
        "temp_kf":0.26},
    "weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
    "clouds":{"all":88},
    "wind":{"speed":5.71,"deg":229.501},
    "sys":{"pod":"d"},
    "dt_txt":"2014-07-23 09:00:00"},
     {
"dt":1406106000,
"main":{
    "temp":298.77,
    "temp_min":298.77,
    "temp_max":298.774,
    "pressure":1005.93,
    "sea_level":1018.18,
    "grnd_level":1005.93,
    "humidity":87,
    "temp_kf":0.26},
"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
"clouds":{"all":88},
"wind":{"speed":5.71,"deg":229.501},
"sys":{"pod":"d"},
"dt_txt":"2014-07-23 09:00:00"}
]}

I created all the classes needed the "all in one" object is here:

public class AIOobject {
City city;
Coord coord;
String country;
String cod;
String message;
String cnt;
List[] list;

public AIOobject(City city, Coord coord, String country, String cod, String message, String cnt, List[] list) {
    this.city = city;
    this.coord = coord;
    this.country = country;
    this.cod = cod;
    this.message = message;
    this.cnt = cnt;
    this.list = list;
}
}

The other classes are just saving data like:

public class Weather {

String id;
String main;
String description;
String icon;

public Weather(String id, String main, String description, String icon) {
    this.id = id;
    this.main = main;
    this.description = description;
    this.icon = icon;
}
}

My question is now why I get this error and how I get solve the problem. Thanks for all responses

EDIT Fixed the JSON Object

~Paul

City is missing the end curly brace at the end of the first line in the JSON. It should be:

"city":{"id":1851632,"name":"Shuzenji"}

after fixing your JSON, you can try using an auto generator to create your gson file.

below is the auto-created file from http://www.jsonschema2pojo.org

-----------------------------------com.example.AIObject.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AIObject {

@SerializedName("city")
@Expose
private City city;
@SerializedName("coord")
@Expose
private Coord coord;
@SerializedName("country")
@Expose
private String country;
@SerializedName("cod")
@Expose
private String cod;
@SerializedName("message")
@Expose
private Double message;
@SerializedName("cnt")
@Expose
private Integer cnt;
@SerializedName("list")
@Expose
private java.util.List<com.example.List> list = null;

public City getCity() {
return city;
}

public void setCity(City city) {
this.city = city;
}

public Coord getCoord() {
return coord;
}

public void setCoord(Coord coord) {
this.coord = coord;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getCod() {
return cod;
}

public void setCod(String cod) {
this.cod = cod;
}

public Double getMessage() {
return message;
}

public void setMessage(Double message) {
this.message = message;
}

public Integer getCnt() {
return cnt;
}

public void setCnt(Integer cnt) {
this.cnt = cnt;
}

public java.util.List<com.example.List> getList() {
return list;
}

public void setList(java.util.List<com.example.List> list) {
this.list = list;
}

}
-----------------------------------com.example.City.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class City {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
-----------------------------------com.example.Clouds.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Clouds {

@SerializedName("all")
@Expose
private Integer all;

public Integer getAll() {
return all;
}

public void setAll(Integer all) {
this.all = all;
}

}
-----------------------------------com.example.Coord.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Coord {

@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("lat")
@Expose
private Double lat;

public Double getLon() {
return lon;
}

public void setLon(Double lon) {
this.lon = lon;
}

public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

}
-----------------------------------com.example.List.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class List {

@SerializedName("dt")
@Expose
private Integer dt;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("weather")
@Expose
private java.util.List<Weather> weather = null;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("dt_txt")
@Expose
private String dtTxt;

public Integer getDt() {
return dt;
}

public void setDt(Integer dt) {
this.dt = dt;
}

public Main getMain() {
return main;
}

public void setMain(Main main) {
this.main = main;
}

public java.util.List<Weather> getWeather() {
return weather;
}

public void setWeather(java.util.List<Weather> weather) {
this.weather = weather;
}

public Clouds getClouds() {
return clouds;
}

public void setClouds(Clouds clouds) {
this.clouds = clouds;
}

public Wind getWind() {
return wind;
}

public void setWind(Wind wind) {
this.wind = wind;
}

public Sys getSys() {
return sys;
}

public void setSys(Sys sys) {
this.sys = sys;
}

public String getDtTxt() {
return dtTxt;
}

public void setDtTxt(String dtTxt) {
this.dtTxt = dtTxt;
}

}
-----------------------------------com.example.Main.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Main {

@SerializedName("temp")
@Expose
private Double temp;
@SerializedName("temp_min")
@Expose
private Double tempMin;
@SerializedName("temp_max")
@Expose
private Double tempMax;
@SerializedName("pressure")
@Expose
private Double pressure;
@SerializedName("sea_level")
@Expose
private Double seaLevel;
@SerializedName("grnd_level")
@Expose
private Double grndLevel;
@SerializedName("humidity")
@Expose
private Integer humidity;
@SerializedName("temp_kf")
@Expose
private Double tempKf;

public Double getTemp() {
return temp;
}

public void setTemp(Double temp) {
this.temp = temp;
}

public Double getTempMin() {
return tempMin;
}

public void setTempMin(Double tempMin) {
this.tempMin = tempMin;
}

public Double getTempMax() {
return tempMax;
}

public void setTempMax(Double tempMax) {
this.tempMax = tempMax;
}

public Double getPressure() {
return pressure;
}

public void setPressure(Double pressure) {
this.pressure = pressure;
}

public Double getSeaLevel() {
return seaLevel;
}

public void setSeaLevel(Double seaLevel) {
this.seaLevel = seaLevel;
}

public Double getGrndLevel() {
return grndLevel;
}

public void setGrndLevel(Double grndLevel) {
this.grndLevel = grndLevel;
}

public Integer getHumidity() {
return humidity;
}

public void setHumidity(Integer humidity) {
this.humidity = humidity;
}

public Double getTempKf() {
return tempKf;
}

public void setTempKf(Double tempKf) {
this.tempKf = tempKf;
}

}
-----------------------------------com.example.Sys.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Sys {

@SerializedName("pod")
@Expose
private String pod;

public String getPod() {
return pod;
}

public void setPod(String pod) {
this.pod = pod;
}

}
-----------------------------------com.example.Weather.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Weather {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("main")
@Expose
private String main;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getMain() {
return main;
}

public void setMain(String main) {
this.main = main;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

}
-----------------------------------com.example.Wind.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Wind {

@SerializedName("speed")
@Expose
private Double speed;
@SerializedName("deg")
@Expose
private Double deg;

public Double getSpeed() {
return speed;
}

public void setSpeed(Double speed) {
this.speed = speed;
}

public Double getDeg() {
return deg;
}

public void setDeg(Double deg) {
this.deg = deg;
}

}

You are trying to parse the "list" JSONArray as an array. GSON can only convert JSONArrays into a List and that's why changing list form List[] to ArrayList<List> would fix the issue.

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