简体   繁体   中英

Unable to map the latitude and longitude posistions

Edit:

Have added the relevant classes to be able to access each element layer of the JSON array.

Currently when (still) trying to access the location I am calling a new object of the datawrapper class, I can see how the implementation of the code to access the location should work but at the moment I'm receiving the error of:

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

I am getting Eclipse to autocomplete the location object by showing 'getLongitude()' and 'getLatitude()' methods but they should be 'getLat()' and 'getLng()' methods.

I see how accessing the objects in order is allowing me to get the long and lat but still the error above has thrown me.

Here are my serpate JSON classes as they stand:

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

}

Geometry Class:

package com.example.restfulweb;

public class Geometry {
private Location location;

public Location getLocation() {
    return location;
}

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

}

Location class:

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

} }

As shown all Getter and setter methods match. As its a list of the returned objects Im not sure why this error is being thrown?

How the code stands to access the layers:

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

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

Your mappings are off, in that the hierarchy of data mapped in your target class does not match the JSON structure. The name and location fields are at different levels in the JSON, and neither of those fields is at the root level from which you are mapping. If you want to serialize using 'strong types', you need to define a few more classes. Something closer to this:

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
}

Using versions of these classes, deserializing the JSON data into the DataWrapper class should now populate down the object hierarchy, in a manner matching the natural hierarchy of the data. You can retrieve location data with code similar to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM