代码之家  ›  专栏  ›  技术社区  ›  BluPxl

使用GSON转换

  •  0
  • BluPxl  · 技术社区  · 7 年前

    我目前正在开发一种天气应用程序。因此,我必须解析一个JSON对象。我用GSON来做这个。 我总是会出错。

    com。谷歌。格森。JsonSyntaxException:java。lang.IllegalStateException:应为BEGIN\u对象,但为 在第1行第224列path$处开始\u数组。列表[0]。天气

    JSON对象如下所示:

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

    我创建了所需的所有类“all-in-one”对象位于此处:

    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;
    }
    }
    

    其他类只保存以下数据:

    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;
    }
    }
    

    我现在的问题是,为什么会出现这个错误,以及如何解决这个问题。 感谢所有回复

    编辑 修复了JSON对象

    ~保罗

    3 回复  |  直到 7 年前
        1
  •  1
  •   Angel Koh Sergey Kalinichenko    7 年前

    修复JSON后,可以尝试使用自动生成器创建gson文件。

    以下是自动创建的文件 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;
    }
    
    }
    
        2
  •  0
  •   Shane Monks O'Byrne    7 年前

    City缺少JSON中第一行末尾的大括号。它应该是:

    "city":{"id":1851632,"name":"Shuzenji"}
    
        3
  •  0
  •   Rishabh Jain    7 年前

    您正试图将“list”JSONArray解析为一个数组。GSON只能将JSONArrays转换为列表,这就是为什么要更改列表形式 List[] ArrayList<List> 将解决此问题。