首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GSON fromJSON有问题

GSON fromJSON有问题
EN

Stack Overflow用户
提问于 2015-05-23 03:16:29
回答 1查看 249关注 0票数 0

我有一个POJO类,如下所示。

代码语言:javascript
复制
import java.util.List;


public class LocationInfoDTO{
    private int _id;
    private String name;
    private String type;
    private String fullName;
    private int location_id;
    private boolean isEurope;
    private String countryCode;
    private boolean coreCountry;
    private int iataAirportCode;

    private List<Geospatial> geo_location;

    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public int getLocation_id() {
        return location_id;
    }

    public void setLocation_id(int location_id) {
        this.location_id = location_id;
    }

    public boolean isEurope() {
        return isEurope;
    }

    public void setEurope(boolean isEurope) {
        this.isEurope = isEurope;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public boolean isCoreCountry() {
        return coreCountry;
    }

    public void setCoreCountry(boolean coreCountry) {
        this.coreCountry = coreCountry;
    }

    public int getIataAirportCode() {
        return iataAirportCode;
    }

    public void getIataAirportCode(int iataAirportCode) {
        this.iataAirportCode = iataAirportCode;
    }

    @Override
    public String toString() {
        return "LocationInfoDTO [_id=" + _id + ", name=" + name + ", type="
                + type + ", fullName=" + fullName + ", location_id="
                + location_id + ", isEurope=" + isEurope + ", countryCode="
                + countryCode + ", coreCountry=" + coreCountry
                + ", iataAirportCode=" + iataAirportCode + ", geo_location="
                + geo_location + "]";
    }

    public List<Geospatial> getGeo_location() {
        return geo_location;
    }

    public void setGeo_location(List<Geospatial> geo_location) {
        this.geo_location = geo_location;
    }
}

只有一件事,地理空间属性有纬度和经度,那就是不同的POJO class.All在使用GSON fromJSON时发生得很好,除了geo-locationnull

代码语言:javascript
复制
{"_id":376217,"key":null,"name":"test","fullName":"test, test","geo_position":{"latitude":52.52437,"longitude":13.41053}}

这是一个样例JSON。

以这种方式触发

代码语言:javascript
复制
GsonBuilder gsonBuilder = new GsonBuilder();

        Gson gson = gsonBuilder.create();

        JSONArray array = new JSONArray(json);

        for(int i=0;i<array.length();i++){
            System.out.println("Array:"+gson.fromJson(array.getJSONObject(i).toString(), LocationInfoDTO.class));

编辑

geo-location更改为geo-position

代码语言:javascript
复制
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
EN

回答 1

Stack Overflow用户

发布于 2015-05-23 03:35:55

这个错误准确地说明了哪里出了问题。那里有一个用于geo-location的json对象,它需要一个列表。即使它是一个单元素列表。变化

代码语言:javascript
复制
{"_id":376217,"key":null,"name":"test","fullName":"test, test","geo_position":{"latitude":52.52437,"longitude":13.41053}}

代码语言:javascript
复制
{"_id":376217,"key":null,"name":"test","fullName":"test, test","geo_position":[{"latitude":52.52437,"longitude":13.41053}]}

更新

因为总是有一个lat/long,所以您应该更新POJO,以便geo_location是单个GeoSpatial对象,而不是List<GeoSpatial>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30404327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档