2013-01-19 43 views
1

我有一個名爲Route的對象,它存儲RouteStep對象的ArrayList。包含ArrayList語法異常的Gson對象

RouteStep對象存儲和int[]的ArrayList。

這裏是一個類的例子。它們包含其他領域,但我讓他們在這裏省略:

public class Route { 

    @Expose private long routeId; 
    @Expose private List<RouteStep> routeSteps; 

    public Route() {} 

    public long getRouteId() { 
     return routeId; 
    } 

    public void setRouteId(long routeId) { 
     this.routeId = routeId; 
    } 


    public List<RouteStep> getRouteSteps() { 
     return routeSteps; 
    } 

    public void setRouteSteps(List<RouteStep> routeSteps) { 
     this.routeSteps = routeSteps; 
    } 
} 

public class RouteStep { 

    @Expose private ArrayList<int[]> coordinates = new ArrayList<int[]>(); 

    public RouteStep() {} 

    public ArrayList<int[]> getCoordinates() { 
     return coordinates; 
    } 

    public void setCoordinates(ArrayList<int[]> coordinates) { 
     this.coordinates = coordinates; 
    }  
} 

下面是需要被解析JSON的示例:

[ 
    { 
     "routeId": -1, 
     "user": "aa", 
     "date": "08/01/2013 18:20:49", 
     "routeName": "route 1", 
     "distance": 536.4938, 
     "routeSteps": [ 
      { 
       "type": "LineString", 
       "coordinates": [ 
        [ 
         55.940847, 
         -3.182992000000008 
        ], 
        [ 
         55.941983999999984, 
         -3.186577000000001 
        ], 
        [ 
         55.94265899999998, 
         -3.19088 
        ] 
       ] 
      } 
     ] 
    }, 
    { 
     "routeId": -2, 
     "user": "aa", 
     "date": "08/01/2013 18:21:39", 
     "routeName": "route 2", 
     "distance": 455.127, 
     "routeSteps": [ 
      { 
       "type": "LineString", 
       "coordinates": [ 
        [ 
         55.94265899999998, 
         -3.19088 
        ], 
        [ 
         55.942732999999976, 
         -3.191536000000003 
        ], 
        [ 
         55.94519300000003, 
         -3.19124800000001 
        ], 
        [ 
         55.946433, 
         -3.191014999999997 
        ] 
       ] 
      } 
     ] 
    } 
] 

我有這個在我的課這是爲了解析Json:

Gson gson = new Gson(); 

     try { 
      routes = gson.fromJson(json, Route[].class);     
     } catch(JsonSyntaxException e1) { 
      Log.e(TAG, "Syntax exception in retrieved JSON"); 
     } catch(JsonParseException e2) { 
      Log.e(TAG, "Exception occured parsing retrieved JSON"); 
     } 

運行時我總是收到JsonSyntaxExceptions。我已經使用相同的代碼來分析包含基本類型ArrayLists的對象數組,例如int[],但是我不知道如何在ArrayList包含我自己類型的對象時如何執行它。

回答

3

發佈您的實際堆棧跟蹤會忍不住......你:

private ArrayList<int[]> coordinates = new ArrayList<int[]>(); 

,並試圖解析:

[ 
    55.940847, 
    -3.182992000000008 
], 

這些都不是int秒。修復它,它應該工作正常。