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