2012-04-20 101 views
2

一个例子JSON对象如下:使用GSON给错误预期BEGIN_ARRAY但STRING

[{"Title":"John Doe","Address":{"AddressLines":["The Place","123 New Place","London","England"],"Postcode":"NW7 XXY"},"Telephone":"0","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),因为服务返回了多个位置,尽管在上面的示例中我只包含了一个。任何帮助,为什么发生这是非常赞赏。我已经看过这里的一些类似错误,但没有提供任何修复似乎适用于我。

回答

5
{ 
    "finally":[ 
     { 
      "Title":"John Doe", 
      "Address": { 
       "AddressLines":[ 
        "The Place", 
        "123 New Place", 
        "London", 
        "England" 
       ], 
       "Postcode":"NW7XXY" 
      }, 
      "Telephone":"0", 
      "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; 
    } 
} 

那是因为你的字符串开始[当你分析这种类型的Json与GSON的,那么你需要一个标签前缀到它只是我像这样({"finally": your data })。

其实GSON试图映射标签和它的价值,但在你的情况你[犯规包含标签由GSON可以映射。

+0

我要试试这个。不过,我认为它可能是做的地址我已经解析没有前缀的标签上面显示的形式类似的数据。感谢您的意见,但我会尝试这个,并让它知道它是否有效。 – kushaldsouza 2012-04-20 04:15:21

+0

我确实设法使用这种方法来使它工作。我不太清楚为什么其他方法会给出错误。感谢您的帮助 – kushaldsouza 2012-04-20 11:11:19

+0

嗨,我知道这是非常老了,但我有短了同样的问题,你能解释一下你的代码位?解析命令在哪里进行? mainData.class是解析器传递的参数吗? – 2015-06-11 06:40:39

相关问题