java - 无法绘制纬度和经度位置

标签 java android json api google-maps

编辑:

添加了相关类,以便能够访问 JSON 数组的每个元素层。

当前,当(仍然)尝试访问该位置时,我正在调用数据包装器类的新对象,我可以看到访问该位置的代码的实现应该如何工作,但目前我收到以下错误:

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

我让 Eclipse 通过显示“getLongitude()”和“getLatitude()”方法来自动完成位置对象,但它们应该是“getLat()”和“getLng()”方法。

我看到按顺序访问对象如何让我获得经度和纬度,但上面的错误仍然让我困惑。

这是我的蛇形 JSON 类:

数据包装器:

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);
}

}

地理名称类:

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

最佳答案

您的映射已关闭,因为目标类中映射的数据层次结构与 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();

关于java - 无法绘制纬度和经度位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15076628/

相关文章:

ios - 在 Swift 中使用远程 JSON 数据填充 tableView

java - 使用 Play 将验证错误作为 JSON 返回!框架

java - 2 带字符的 For 循环跳过了很多输出

带有奇怪请求的 Java 递归

android - 在推送通知上打开浏览器

java - DataSnapshot for 循环 (Android Studio) 未附加 Firebase 数据库路径的子项

java - 打印数组?

java - 以给定的执行时间启动线程

android - 如何将 2 个 imageViews 放在另一个之上,将它们混合在一起?

c# - 为什么 Request.Content.ReadAsAsync 不区分大小写?