java 使用 GSON 给出错误预期为 BEGIN_ARRAY 但为 STRING

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10239969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:10:33  来源:igfitidea点击:

Using GSON giving error expected BEGIN_ARRAY but was STRING

javaandroidjsongson

提问by kushaldsouza

An example JSON object is shown below:

下面显示了一个示例 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}]

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

假设通过链接 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;
    }

}

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.

预期 BEGIN_ARRAY 但在上述代码行中是字符串。我不确定我设置课程的方式是否有问题。注意:我使用的是一个数组 (LocationData[] 数据),因为该服务返回多个位置,尽管我刚刚在上面显示的示例中包含了一个。任何有关为什么会发生这种情况的帮助都非常感谢。我在这里查看了一些类似的错误,但提供的修复程序似乎对我不起作用。

回答by Mohammed Azharuddin Shaikh

{
    "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 :

解析这个 JSON 的代码是:

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 }).

这是因为[当你用 Gson 解析这种类型的 Json 时,你的字符串开始,然后你需要在它前面加上一个标签,就像我一样({"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 试图映射标签及其值,但在您的情况下,您[不包含 Gson 可以映射的标签。