Gson对象包含ArrayList语法异常

时间:2013-01-19 15:19:42

标签: android json gson

我有一个名为Route的对象,它存储一个RouteStep对象的ArrayList。

RouteStep对象存储int[]

以下是课程示例。它们包含其他字段,但我在此处省略了它们:

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。我使用相同的代码来解析包含原始类型的数组列表的对象数组,例如int[]但是当ArrayList包含我自己类型的对象时,我不知道该怎么做。

1 个答案:

答案 0 :(得分:3)

发布实际的堆栈跟踪会有所帮助,但是......你有:

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

正在尝试解析:

[
    55.940847,
    -3.182992000000008
],

那些不是int。修复它,它应该工作正常。