简体   繁体   中英

Gson JsonSyntaxException: java.lang.NumberFormatException

I am having error while parsing a Json with Gson into an object. What I am trying to do is store the latitude longitude in the double variable but while parsing, I am getting an error. I try to change the class variables and the gson to string but still getting the same error. The json it´s on github.

The error that i get is:

com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Expected an int but was 32.93644801 at line 7 column 22 path $.bars[0].lat

json data is:

{
    "bars":[
        {
            "name": "Antares Pichincha",
            "place_id": "ChIJOR29Pk-rt5UR0hsdFVQWpD8",
            "logo_src": "antares",
            "lat": -32.936448000001,
            "lng": -60.6587110
        }
    ]
}

and the class reciving the data is

public class BarList {

    //@SerializedName("bars")
    List<Bar> bars;

    public List<Bar> getBars(){
        return bars;
    }
}

public class Bar {

    private String name;
    private String place_id;
    private String logo_src;
    private double lat;
    private double lng;

    public Bar(String name, String place_id, String logo_src, double lat, double lng) {
        this.name = name;
        this.place_id = place_id;
        this.logo_src = logo_src;
        this.lat = lat;
        this.lng = lng;
    }

and here is where I call it

GsonRequest<BarList> request = new GsonRequest<BarList>(
                JSON_URL,
                BarList.class,
                null,
                new Response.Listener<BarList>() {
                    @Override
                    public void onResponse(BarList response) {
                        barList = response;
                        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                                .findFragmentById(R.id.map);
                        mapFragment.getMapAsync(MapsActivity.this);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d(TAG, "Error Respuesta en JSON: " + error.getMessage());
                    }
                }
        );

Someone have any idea how can i solve this?

不确定这是否是您的问题,但是bar数组仅包含一个元素,但第9行是逗号,后面应跟另一个元素。

I ask to a developer friend to take a look at the code and he could not find any problems or errors on the code, so he told me to delete the installed app on the device and re-build the project and that solve the issue.

Thanks everyone for the answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM