Emre Koç
Emre Koç

Reputation: 1381

Exception in JSON parsing: Expected BEGIN_ARRAY but was BEGIN_OBJECT?

I am trying to parse the JSON response from this link and I'm getting this exception:

Expected BEGIN_ARRAY but was BEGIN_OBJECT

I have created this class to encapsulate the JSON data:

public class PlacesTextSearch {

    private String icon;
    private String name;
    private String types;
    private String formatted_address;
    private double latitude;
    private double longitude;
    private String id;

    public PlacesTextSearch(String icon,String name,String types,String formatted_address,double latitude,double longitude) {
        // TODO Auto-generated constructor stub
        setIcon(icon);
        setName(name);
        setTypes(types);
        setFormatted_address(formatted_address);
        setLatitude(latitude);
        setLongitude(longitude);
    }

        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getIcon() {
            return icon;
        }
        public void setIcon(String icon) {
            this.icon = icon;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getTypes() {
            return types;
        }
        public void setTypes(String types) {
            this.types = types;
        }
        public String getFormatted_address() {
            return formatted_address;
        }
        public void setFormatted_address(String formatted_address) {
            this.formatted_address = formatted_address;
        }
        public Double getLatitude() {
            return latitude;
        }
        public void setLatitude(Double latitude) {
            this.latitude = latitude;
        }
        public Double getLongitude() {
            return longitude;
        }
        public void setLongitude(Double longitude) {
            this.longitude = longitude;
        }
}

And this is my code to parse the JSON:

private ArrayList<PlacesTextSearch> arrayListPlaces;       
Type listType = new TypeToken<ArrayList<PlacesTextSearch>>(){}.getType();
arrayListPlaces=new Gson().fromJson(response,listType);

Here you can see the complete exception stacktrace:

Complete exception stacktrace

Upvotes: 1

Views: 2607

Answers (1)

MikO
MikO

Reputation: 18741

Have you read Gson documentation before trying to write your code? Have you even taken a look at JSON structures?

Your code has many errors... The point is that you have to create a Java class structure that matches your JSON structure. And in fact, your class structure is not even similar to the JSON you want to parse! Basically where there's an object { } in your JSON you have to use a class, and where there's an array in your JSON [ ] you have to use an array or a List...

According to your PlacesTextSearch class, I guess the JSON piece you want to parse is:

{    
  ...,
  "results" : [
    {
      "formatted_address" : "Zeytinlik Mh., Bakırköy/İstanbul, Türkiye",
      "geometry" : {
        "location" : {
          "lat" : 40.9790040,
          "lng" : 28.8730110
        }
      },
      "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
      "id" : "e520b6e19bae5c014470989f9d3405e55dce5155",
      "name" : "PTT",
      "types" : [ "post_office", "finance", "establishment" ]
      ...
    },
    ...
  ],
  ...
}

So, how do you pretend to parse this into an ArrayList<PlacesTextSearch>!? That's not what your JSON represents! Do you really not see it?

Try something like this class structure (pseudo-code):

class Response
  List<PlacesTextSearch> results;

class PlacesTextSearch
  String formatted_address;
  Geometry geometry;
  String icon;
  String id;
  String name;
  List<String> types;

class Geometry
  Location location;

class Location
  long lat;
  long lng;

And parse it with:

Response response = new Gson().fromJson(response, Response.class);

Upvotes: 3

Related Questions