JSON解析中的异常:预期BEGIN_ARRAY但是BEGIN_OBJECT?

时间:2013-08-13 12:03:32

标签: java android json web-services gson

我正在尝试解析the JSON response from this link并且我遇到了这个例外:

Expected BEGIN_ARRAY but was BEGIN_OBJECT

我创建了这个类来封装JSON数据:

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;
        }
}

这是我解析JSON的代码:

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

在这里,您可以看到完整的异常堆栈跟踪:

Complete exception stacktrace

1 个答案:

答案 0 :(得分:3)

在尝试编写代码之前,您是否阅读过Gson documentation?你有没看过JSON structures

您的代码有很多错误......重点是您必须创建一个与您的JSON结构相匹配的Java类结构。实际上,您的类结构甚至与您要解析的JSON类似!基本上你的JSON中有一个对象{ },你必须使用一个类,而你的JSON [ ]中有一个数组,你必须使用数组或List ... < / p>

根据你的PlacesTextSearch课程,我猜你要解析的JSON片段是:

{    
  ...,
  "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" ]
      ...
    },
    ...
  ],
  ...
}

那么,你如何假装将其解析为ArrayList<PlacesTextSearch> !?这不是你的JSON所代表的!你真的没有看到它吗?

尝试类似这样的类结构(伪代码):

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;

并解析:

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