使用GSON给错误预期BEGIN_ARRAY但STRING [英] Using GSON giving error expected BEGIN_ARRAY but was STRING

查看:166
本文介绍了使用GSON给错误预期BEGIN_ARRAY但STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是例子JSON对象,如下所示:

  [{标题:李四,地址:{AddressLines:世贸天阶,123新的地方,伦敦,英伦],邮报code:NW7 XXY"},"Telephone":"0012345","Email":"","Latitude":51.5024472101345,"Longitude":-0.557585646554,"Easting":500623,"Northing":179647}]
 

假设上述目的是通过链接www.domain.com访问,我有下面的类重新present数据

 公共类LocationData扩展数据{

    私有地址地址;
    私人字符串电话;
    私人字符串电子邮件;
    私人字符串纬度;
    私人字符串经度;
    私人字符串东距;
    私人字符串北向;

    公共地址的getAddress(){
        退货地址;
    }

    公共无效setAddress(地址地址){
        地址=地址;
    }

    公共字符串getTelephone(){
        回电话;
    }

    公共无效setTelephone(字符串电话){
        电话=电话;
    }

    公共字符串getEmail(){
        返回电子邮件;
    }

    公共无效setEmail(字符串电子邮件){
        电子邮件=电子邮件;
    }

    公共字符串getLatitude(){
        返回纬度;
    }

    公共无效setLatitude(字符串纬度){
        纬度=纬度
    }

    公共字符串getLongitude(){
        返回经度;
    }

    公共无效setLongitude(字符串经度){
        经度=经度
    }

    公共字符串getEasting(){
        返回东距;
    }

    公共无效setEasting(字符串东向){
        东距=东距;
    }

    公共字符串getNorthing(){
        返回北向;
    }

    公共无效setNorthing(字符串北距){
        北距=北距;
    }

}
 

和地址类如下:

 公共类地址{

    公众的String [] AddressLines;

    公共字符串邮报code;

    公共字符串的getPost code(){
        回邮报code;
    }

    公共无效setPost code(字符串后code){
        帖子code =交code;
    }

    公众的String [] getAddressLines(){
        返回AddressLines;
    }

    公共无效setAddressLines(字符串addressLines []){
        AddressLines = addressLines;
    }


}
 

当我尝试运行

  LocationData []数据= gson.fromJson(this.locationServiceUrl,LocationData []类。);
返回的数据;
 

我收到以下错误:

预计BEGIN_ARRAY但串在上述行code。我不知道,如果有什么不对的,其中我已经建立了我的课的方式。注:我使用的是阵列(LocationData []数据),因为该服务将返回多个位置,虽然我刚才包括一名在上面的例子。任何帮助,为什么发生这种情况是非常AP preciated。我看过一些类似的错误在这里,但没有提供修复程序似乎为我工作。

解决方案

  {
    最后:[
        {
            标题:李四,
            地址: {
                AddressLines:[
                    世贸天阶,
                    123新的地方,
                    伦敦,
                    英国
                ]
                邮报code:NW7XXY
            },
            电话:0012345,
            电子邮件:,
            纵横:51.5024472101345,
            经度: -  0.557585646554,
            东向:500623,
            北向:179647
        }
    ]
}
 

和code解析此JSON是:

 公共类mainData {

    公开名单< LocationData>最后;

    公众的String [] getLocationData(){
        返回AddressLines;
    }

    公共无效setLocationData(名单< LocationData>最后){
        this.finally =最后;
    }
}
 

那是因为你的字符串开头 [当你解析这种类型的Json与GSON的,那么你需要preFIX一个标签,它只是我喜欢没有( {终于:你的数据} )。

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

An example JSON object is shown below:

[{"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}]

Suppose the above object is accessed via the link www.domain.com and I have the following class to represent the data

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;
    }

}

And the address class is as follows:

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;
    }


}

When I try to run

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

I get the following error:

Expected BEGIN_ARRAY but was string at the above mentioned line of code. I am not sure if there is something wrong in the manner in which I have set up my classes. Note: I am using an array (LocationData[] data) because the service returns multiple locations although I have just included one in the example shown above. Any help as to why this is happening is much appreciated. I have looked at some of the similar errors on here but none of the fixes provided seem to work for me.

解决方案

{
    "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
        }
    ]
}

and code to parse this JSON is :

public class mainData {

    public List<LocationData> finally;

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

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

it is because your string starting with [ when you parsing this type of Json with Gson then you need to prefix a label to it just i like did ( {"finally": your data }).

Actually Gson trying to map the label and its value but in your case your [ doesnt contain Label by which Gson can map.

这篇关于使用GSON给错误预期BEGIN_ARRAY但STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆