1

編集:

JSON配列の各要素レイヤーにアクセスできるように、関連するクラスを追加しました。

現在、(まだ)場所にアクセスしようとすると、datawrapperクラスの新しいオブジェクトを呼び出していますが、場所にアクセスするためのコードの実装がどのように機能するかを確認できますが、現時点では次のエラーが発生します。

The method getGeometry() is undefined for the type List.

Eclipseに「getLongitude()」および「getLatitude()」メソッドを表示してロケーションオブジェクトをオートコンプリートさせていますが、これらは「getLat()」および「getLng()」メソッドである必要があります。

オブジェクトに順番にアクセスすることで、長くて遅いものを取得できるようになっていることがわかりますが、それでも上記のエラーが原因で発生します。

現状の私のserpateJSONクラスは次のとおりです。

データラッパー:

package com.example.restfulweb;

import java.util.List;

import com.google.gson.Gson;

public class DataWrapper<GeoResult> {

    List<GeoName> results;

public List<GeoName> getResults() {
    return results;
}

public void setResults(List<GeoName> results) {
    this.results = results;
}

@SuppressWarnings("rawtypes")
public DataWrapper fromJson(String jsonString)
{
    return new Gson().fromJson(jsonString, DataWrapper.class);
}

}

GeoNameクラス:

package com.example.restfulweb;

public class GeoName {

private String id;
private Geometry geometry;
private String name;

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public Geometry getGeometry() {
    return geometry;
}
public void setGeometry(Geometry geometry) {
    this.geometry = geometry;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

ジオメトリクラス:

package com.example.restfulweb;

public class Geometry {
private Location location;

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

}

ロケーションクラス:

パッケージcom.example.restfulweb;

public class Location {
private Double lat;
private Double lng;

public Double getLat() {
    return lat;
}
public void setLat(Double lat) {
    this.lat = lat;
}
public Double getLng() {
    return lng;
}
public void setLng(Double lng) {
    this.lng = lng;
}

}}

示されているように、すべてのGetterメソッドとsetterメソッドは一致します。返されたオブジェクトのリストとして、なぜこのエラーがスローされているのかわかりませんか?

レイヤーにアクセスするためのコードの状態:

@SuppressWarnings("rawtypes")
        DataWrapper dataWrapper = new DataWrapper();

        Location location = dataWrapper.getResults().getGeometry().getLocation();
4

1 に答える 1

0

ターゲットクラスにマッピングされたデータの階層がJSON構造と一致しないため、マッピングはオフになっています。nameとフィールドはJSONのlocation異なるレベルにあり、これらのフィールドはどちらもマッピング元のルートレベルにありません。「強力な型」を使用してシリアル化する場合は、さらにいくつかのクラスを定義する必要があります。これに近いもの:

public class Location {
    private Double lat;
    private Double lng;

    // Constructors, getters, setters
}

public class Geometry {
    private Location location;

    // Constructors, getters, setters
}

public class GeoResult {
    private String id;
    private Geometry geometry;
    private String name;

    // Constructors, getters, setters
}

public class DataWrapper {
    private List<GeoResult> results;

    // Constructors, getters, setters
}

これらのクラスのバージョンを使用して、JSONデータをクラスに逆シリアル化するDataWrapperと、データの自然な階層と一致する方法で、オブジェクト階層にデータが入力されるようになります。次のようなコードで位置データを取得できます。

Location location = dataWrapper.getResults(0).getGeometry().getLocation();
于 2013-02-25T21:50:37.100 に答える