Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

java - Using GSON giving error expected BEGIN_ARRAY but was STRING

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
{
    "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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...