编辑:无法映射经度和纬度位置
已添加相关类以便能够访问JSON数组的每个元素层。
当前(仍然)尝试访问我正在调用datawrapper类的新对象的位置时,我可以看到访问该位置的代码的实现应该如何工作,但此刻我收到错误的:
The method getGeometry() is undefined for the type List.
我得到的Eclipse通过展示 'getLongitude()' 和 'getLatitude()' 方法,就能自动定位对象,但他们应该是 'getLat()' 和 'getLng()' 方法。
我看到如何访问对象的顺序是让我得到长期和拉特但仍然上面的错误已经抛出我。
这里是我的serpate JSON类因为他们的立场:
Datawrapper:
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;
}
}
Geometry类:
package com.example.restfulweb;
public class Geometry {
private Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = 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();
您可以添加从JSON加载GeoName对象的代码吗? – ianhanniballake 2013-02-25 21:21:54
@ianhanniballake我已经添加了用于返回用于映射的JSON结果对象的方法。 – user1352057 2013-02-25 21:25:50