使用GSON给出错误预期BEGIN_ARRAY但是STRING

时间:2012-04-20 03:47:06

标签: java android json gson

示例JSON对象如下所示:

[{"Title":"John Doe","Address":{"AddressLines":["The Place","123 New Place","London","England"],"Postcode":"NW7 XXY"},"Telephone":"0012345","Email":"","Latitude":51.5024472101345,"Longitude":-0.557585646554,"Easting":500623,"Northing":179647}]

假设通过链接www.domain.com访问上述对象,我有以下类来表示数据

public class LocationData extends Data{

    private Address Address;
    private String Telephone;
    private String Email;
    private String Latitude;
    private String Longitude;
    private String Easting;
    private String Northing;

    public Address getAddress() {
        return Address;
    }

    public void setAddress(Address address) {
        Address = address;
    }

    public String getTelephone() {
        return Telephone;
    }

    public void setTelephone(String telephone) {
        Telephone = telephone;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getLatitude() {
        return Latitude;
    }

    public void setLatitude(String latitude) {
        Latitude = latitude;
    }

    public String getLongitude() {
        return Longitude;
    }

    public void setLongitude(String longitude) {
        Longitude = longitude;
    }

    public String getEasting() {
        return Easting;
    }

    public void setEasting(String easting) {
        Easting = easting;
    }

    public String getNorthing() {
        return Northing;
    }

    public void setNorthing(String northing) {
        Northing = northing;
    }

}

地址类如下:

public class Address {

    public String[] AddressLines;

    public String Postcode;

    public String getPostcode() {
        return Postcode;
    }

    public void setPostcode(String postcode) {
        Postcode = postcode;
    }

    public String[] getAddressLines() {
        return AddressLines;
    }

    public void setAddressLines(String addressLines[]) {
        AddressLines = addressLines;
    }


}

当我尝试运行时

LocationData[] data = gson.fromJson(this.locationServiceUrl, LocationData[].class);
return data;

我收到以下错误:

预计BEGIN_ARRAY但是在上面提到的代码行中是字符串。我不确定我设置课程的方式是否有问题。注意:我正在使用数组(LocationData []数据),因为该服务返回多个位置,尽管我刚刚在上面显示的示例中包含了一个。任何关于为什么会发生这种情况的帮助非常感谢。我在这里看了一些类似的错误,但没有提供的修复似乎对我有用。

1 个答案:

答案 0 :(得分:5)

{
    "finally":[
        {
            "Title":"John Doe",
            "Address": {
                "AddressLines":[
                    "The Place",
                    "123 New Place",
                    "London",
                    "England"
                ],
                "Postcode":"NW7XXY"
            },
            "Telephone":"0012345",
            "Email":"",
            "Latitude":51.5024472101345,
            "Longitude":-0.557585646554,
            "Easting":500623,
            "Northing":179647
        }
    ]
}

和解析此JSON的代码是:

public class mainData {

    public List<LocationData> finally;

    public String[] getLocationData() {
        return AddressLines;
    }

    public void setLocationData(List<LocationData> finally) {
        this.finally = finally;
    }
}

这是因为当你使用Gson解析这种类型的Json时,你的字符串以[开头,那么你需要在标签前加上我喜欢的({"finally": your data })。

实际上Gson试图映射标签及其值,但在你的情况下,你的[并不包含Gson可以映射的标签。