2

示例 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[] data),因为服务返回多个位置,尽管我在上面显示的示例中只包含了一个。非常感谢任何有关为什么会发生这种情况的帮助。我在这里查看了一些类似的错误,但提供的修复似乎都不适合我。

4

1 回答 1

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 可以映射的标签。

于 2012-04-20T04:01:43.393 回答