一尘不染

使用GSON给出错误,预计为BEGIN_ARRAY,但为STRING

json

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
[]数据),因为该服务返回了多个位置,尽管我刚刚在上面显示的示例中包括了一个。感谢您提供任何有关为什么发生这种情况的帮助。我已经看过这里的一些类似错误,但是所提供的修复程序似乎都不适合我。


阅读 210

收藏
2020-07-27

共1个答案

一尘不染

{
    "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可以通过其映射的Label。

2020-07-27