編集:
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();