1

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

1
  • 2
    apparently you are trying to load a json object into an array. may be you could post the actual json thing Commented Aug 13, 2013 at 12:25

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry, I forgot put some codes into my question. I get results from my JSON with this code object = new JSONObject(response); array = object.getJSONArray("results"); i am not trying to get all things. But you are right, forgetting somethings is my fault. Thank you for answer
@EmreKoç: If you use my simple class structure it should be working for you, isn't it?
Yes it should work but i dont need all data which is coming from JSON. I just need results and i modified my code for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.