2013-02-25 85 views
1

编辑:无法映射经度和纬度位置

已添加相关类以便能够访问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(); 
+1

您可以添加从JSON加载GeoName对象的代码吗? – ianhanniballake 2013-02-25 21:21:54

+0

@ianhanniballake我已经添加了用于返回用于映射的JSON结果对象的方法。 – user1352057 2013-02-25 21:25:50

回答

0

你的映射是关闭的,因为在你的目标类不匹配的JSON结构映射数据的层次结构。 namelocation字段在JSON中处于不同的级别,并且这些字段都不在您要映射的根级别。如果你想使用'强类型'序列化,你需要定义几个类。更接近于这样的:

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(); 
+0

很好的答案谢谢。只是试图让我的头靠近这一切。所以基本上我需要将每个级别的JSON响应分解成一个类?获取和设置每个'属性'即经纬度? – user1352057 2013-02-25 21:54:28

+0

是的,当反序列化为一个类型时,您需要将JSON中的每个级别映射为一个类。 – Perception 2013-02-25 21:57:00

+0

非常感谢,这是有道理的。我可以看到这些类的图层如何链接在一起来生成JSON'图层',但是现在我所坚持的是检索lat和long的方法。我已经把GeoName类中的'geometry'类。我是否需要创建DataWrapper类的新实例来检索经纬度?或者是从该位置对象获取经纬度的情况,即location.lat和location.lng? – user1352057 2013-02-26 01:53:43