1

RouteStepオブジェクトのArrayListを格納するRouteというオブジェクトがあります。

RouteStepオブジェクトは、のArrayListを格納します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ます。同じコードを使用して、などのプリミティブ型のArrayListを含むオブジェクトの配列を解析int[]しましたが、ArrayListに自分の型のオブジェクトが含まれている場合の方法がわかりません。

4

1 に答える 1

3

実際のスタックトレースを投稿すると役立ちますが...次のようになります。

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

そして、解析しようとしています:

[
    55.940847,
    -3.182992000000008
],

それらはintsではありません。それを修正すれば、正常に動作するはずです。

于 2013-01-19T15:40:40.573 に答える