如何在android应用程序中解析从地理编码API(V3)接收到的Json响应?

如何在android应用程序中解析从地理编码API(V3)接收到的Json响应?,android,org.json,Android,Org.json,我想解析JSON响应中的地址http://maps.googleapis.com/maps/api/geocode/json?address=public+library+san+diego&sensor=从我的android应用程序中的地理编码API接收到错误 有人能帮我解析响应并在列表视图中显示它吗 非常感谢您的帮助。您可以使用来处理JSON响应 gson用户指南给出了如何做到这一点的示例,但基本上您需要创建一个与JSON响应对象的结构匹配的Java类 完成后,您应该有一个某种形状或形式的地

我想解析JSON响应中的地址http://maps.googleapis.com/maps/api/geocode/json?address=public+library+san+diego&sensor=从我的android应用程序中的地理编码API接收到错误

有人能帮我解析响应并在列表视图中显示它吗

非常感谢您的帮助。

您可以使用来处理JSON响应

gson用户指南给出了如何做到这一点的示例,但基本上您需要创建一个与JSON响应对象的结构匹配的Java类

完成后,您应该有一个某种形状或形式的地址对象列表,例如,您可以使用响应的格式化地址属性来初始化ListAdapter。

您可以使用它来处理JSON响应

gson用户指南给出了如何做到这一点的示例,但基本上您需要创建一个与JSON响应对象的结构匹配的Java类


完成后,您应该有一个某种形状或形式的地址对象列表,例如,您可以使用响应的格式化地址属性来初始化ListAdapter。

Android包含json.org库,因此很容易将json解析为对象。有一个关于在Android中使用JSON的非常简短的教程。解析完数据后,只需将其放入ListView的适配器中即可。

Android包含json.org库,因此将json解析为对象非常容易。有一个关于在Android中使用JSON的非常简短的教程。解析数据后,只需将其放入ListView的适配器中即可。

使用以下算法解析结果:

private ArrayList<InfoPoint> parsePoints(String strResponse) {
        // TODO Auto-generated method stub
        ArrayList<InfoPoint> result=new ArrayList<InfoPoint>();
        try {
            JSONObject obj=new JSONObject(strResponse);
            JSONArray array=obj.getJSONArray("results");
            for(int i=0;i<array.length();i++)
            {
                            InfoPoint point=new InfoPoint();

                JSONObject item=array.getJSONObject(i);
                ArrayList<HashMap<String, Object>> tblPoints=new ArrayList<HashMap<String,Object>>();
                JSONArray jsonTblPoints=item.getJSONArray("address_components");
                for(int j=0;j<jsonTblPoints.length();j++)
                {
                    JSONObject jsonTblPoint=jsonTblPoints.getJSONObject(j);
                    HashMap<String, Object> tblPoint=new HashMap<String, Object>();
                    Iterator<String> keys=jsonTblPoint.keys();
                    while(keys.hasNext())
                    {
                        String key=(String) keys.next();
                        if(tblPoint.get(key) instanceof JSONArray)
                        {
                            tblPoint.put(key, jsonTblPoint.getJSONArray(key));
                        }
                        tblPoint.put(key, jsonTblPoint.getString(key));
                    }
                    tblPoints.add(tblPoint);
                }
                point.setAddressFields(tblPoints);
                point.setStrFormattedAddress(item.getString("formatted_address"));
                JSONObject geoJson=item.getJSONObject("geometry");
                JSONObject locJson=geoJson.getJSONObject("location");
                point.setDblLatitude(Double.parseDouble(locJson.getString("lat")));
                point.setDblLongitude(Double.parseDouble(locJson.getString("lng")));

                result.add(point);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }
InfoPoint类的代码为:

import java.util.ArrayList;
import java.util.HashMap;

public class InfoPoint {
    ArrayList<HashMap<String, Object>> addressFields=new ArrayList<HashMap<String, Object>>();
    String strFormattedAddress="";
    double dblLatitude=0;
    double dblLongitude=0;
    public ArrayList<HashMap<String, Object>> getAddressFields() {
        return addressFields;
    }
    public void setAddressFields(ArrayList<HashMap<String, Object>> addressFields) {
        this.addressFields = addressFields;
    }
    public String getStrFormattedAddress() {
        return strFormattedAddress;
    }
    public void setStrFormattedAddress(String strFormattedAddress) {
        this.strFormattedAddress = strFormattedAddress;
    }
    public double getDblLatitude() {
        return dblLatitude;
    }
    public void setDblLatitude(double dblLatitude) {
        this.dblLatitude = dblLatitude;
    }
    public double getDblLongitude() {
        return dblLongitude;
    }
    public void setDblLongitude(double dblLongitude) {
        this.dblLongitude = dblLongitude;
    }

}

使用以下算法分析结果:

private ArrayList<InfoPoint> parsePoints(String strResponse) {
        // TODO Auto-generated method stub
        ArrayList<InfoPoint> result=new ArrayList<InfoPoint>();
        try {
            JSONObject obj=new JSONObject(strResponse);
            JSONArray array=obj.getJSONArray("results");
            for(int i=0;i<array.length();i++)
            {
                            InfoPoint point=new InfoPoint();

                JSONObject item=array.getJSONObject(i);
                ArrayList<HashMap<String, Object>> tblPoints=new ArrayList<HashMap<String,Object>>();
                JSONArray jsonTblPoints=item.getJSONArray("address_components");
                for(int j=0;j<jsonTblPoints.length();j++)
                {
                    JSONObject jsonTblPoint=jsonTblPoints.getJSONObject(j);
                    HashMap<String, Object> tblPoint=new HashMap<String, Object>();
                    Iterator<String> keys=jsonTblPoint.keys();
                    while(keys.hasNext())
                    {
                        String key=(String) keys.next();
                        if(tblPoint.get(key) instanceof JSONArray)
                        {
                            tblPoint.put(key, jsonTblPoint.getJSONArray(key));
                        }
                        tblPoint.put(key, jsonTblPoint.getString(key));
                    }
                    tblPoints.add(tblPoint);
                }
                point.setAddressFields(tblPoints);
                point.setStrFormattedAddress(item.getString("formatted_address"));
                JSONObject geoJson=item.getJSONObject("geometry");
                JSONObject locJson=geoJson.getJSONObject("location");
                point.setDblLatitude(Double.parseDouble(locJson.getString("lat")));
                point.setDblLongitude(Double.parseDouble(locJson.getString("lng")));

                result.add(point);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }
InfoPoint类的代码为:

import java.util.ArrayList;
import java.util.HashMap;

public class InfoPoint {
    ArrayList<HashMap<String, Object>> addressFields=new ArrayList<HashMap<String, Object>>();
    String strFormattedAddress="";
    double dblLatitude=0;
    double dblLongitude=0;
    public ArrayList<HashMap<String, Object>> getAddressFields() {
        return addressFields;
    }
    public void setAddressFields(ArrayList<HashMap<String, Object>> addressFields) {
        this.addressFields = addressFields;
    }
    public String getStrFormattedAddress() {
        return strFormattedAddress;
    }
    public void setStrFormattedAddress(String strFormattedAddress) {
        this.strFormattedAddress = strFormattedAddress;
    }
    public double getDblLatitude() {
        return dblLatitude;
    }
    public void setDblLatitude(double dblLatitude) {
        this.dblLatitude = dblLatitude;
    }
    public double getDblLongitude() {
        return dblLongitude;
    }
    public void setDblLongitude(double dblLongitude) {
        this.dblLongitude = dblLongitude;
    }

}

我对这个解决方案感到困惑了一段时间,我很难找到基于谷歌关键词搜索的解决方案,所以既然我已经解决了这个问题,我决定把我的解决方案放在这里

创建GSon需要从json转换为GeocodeResponse的所有模型

public class GeocodeResponse {
    private String status;
    private List<Geocode> results = new ArrayList<Geocode>();

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setResults(List<Geocode> results) {
        this.results = results;
    }

    public List<Geocode> getResults() {
        return results;
    }
}


public class Geocode {
    private Collection<String> types = new ArrayList<String>();
    private String formatted_address;
    private Collection<AddressComponent> address_components = new ArrayList<AddressComponent>();
    private Geometry geometry;
    private boolean partialMatch;

    public Collection<String> getTypes() {
        return types;
    }

    public void setTypes(Collection<String> types) {
        this.types = types;
    }

    public void setFormatted_address(String formatted_address) {
        this.formatted_address = formatted_address;
    }

    public String getFormatted_address() {
        return formatted_address;
    }

    public void setAddress_components(Collection<AddressComponent> address_components) {
        this.address_components = address_components;
    }

    public Collection<AddressComponent> getAddress_components() {
        return address_components;
    }

    public Geometry getGeometry() {
        return geometry;
    }

    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }

    public boolean isPartialMatch() {
        return partialMatch;
    }

    public void setPartialMatch(boolean partialMatch) {
        this.partialMatch = partialMatch;
    }
}

public class AddressComponent {
    private String longName;
    private String shortName;
    private Collection<String> types = new ArrayList<String>();

    public String getLongName() {
        return longName;
    }

    public void setLongName(String longName) {
        this.longName = longName;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public Collection<String> getTypes() {
        return types;
    }

    public void setTypes(Collection<String> types) {
        this.types = types;
    }
}

public class Geometry {
    private Location location;
    private String locationType;
    private Area viewport;
    private Area bounds;

    public Location getLocation() {
        return location;
    }

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

    public String getLocationType() {
        return locationType;
    }

    public void setLocationType(String locationType) {
        this.locationType = locationType;
    }

    public Area getViewport() {
        return viewport;
    }

    public void setViewport(Area viewport) {
        this.viewport = viewport;
    }

    public Area getBounds() {
        return bounds;
    }

    public void setBounds(Area bounds) {
        this.bounds = bounds;
    }
}


public class Location {
    private double lat;
    private double lng;

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLat() {
        return lat;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public double getLng() {
        return lng;
    }
}


public class Area {
    private Location southWest;
    private Location northEast;

    public Location getSouthWest() {
        return southWest;
    }

    public void setSouthWest(Location southWest) {
        this.southWest = southWest;
    }

    public Location getNorthEast() {
        return northEast;
    }

    public void setNorthEast(Location northEast) {
        this.northEast = northEast;
    }
}
然后尝试以下代码:

@Service
public class RestService {
    private static final String URL = "http://maps.googleapis.com/maps/api/geocode/json?address={address}&sensor=false";

    @Autowired
    private RestTemplate restTemplate;

    public GeocodeResponse getMap(String address) {
        Map<String, String> vars = new HashMap<String, String>();
        vars.put("address", address);

        String json = restTemplate.getForObject(URL,String.class, vars);

        return new Gson().fromJson(json, GeocodeResponse.class);
    }

}

希望能有所帮助。

我对这个解决方案琢磨了一段时间,很难找到基于谷歌关键词搜索的解决方案,所以既然我已经解决了这个问题,我决定把我的解决方案放在这里

创建GSon需要从json转换为GeocodeResponse的所有模型

public class GeocodeResponse {
    private String status;
    private List<Geocode> results = new ArrayList<Geocode>();

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setResults(List<Geocode> results) {
        this.results = results;
    }

    public List<Geocode> getResults() {
        return results;
    }
}


public class Geocode {
    private Collection<String> types = new ArrayList<String>();
    private String formatted_address;
    private Collection<AddressComponent> address_components = new ArrayList<AddressComponent>();
    private Geometry geometry;
    private boolean partialMatch;

    public Collection<String> getTypes() {
        return types;
    }

    public void setTypes(Collection<String> types) {
        this.types = types;
    }

    public void setFormatted_address(String formatted_address) {
        this.formatted_address = formatted_address;
    }

    public String getFormatted_address() {
        return formatted_address;
    }

    public void setAddress_components(Collection<AddressComponent> address_components) {
        this.address_components = address_components;
    }

    public Collection<AddressComponent> getAddress_components() {
        return address_components;
    }

    public Geometry getGeometry() {
        return geometry;
    }

    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }

    public boolean isPartialMatch() {
        return partialMatch;
    }

    public void setPartialMatch(boolean partialMatch) {
        this.partialMatch = partialMatch;
    }
}

public class AddressComponent {
    private String longName;
    private String shortName;
    private Collection<String> types = new ArrayList<String>();

    public String getLongName() {
        return longName;
    }

    public void setLongName(String longName) {
        this.longName = longName;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public Collection<String> getTypes() {
        return types;
    }

    public void setTypes(Collection<String> types) {
        this.types = types;
    }
}

public class Geometry {
    private Location location;
    private String locationType;
    private Area viewport;
    private Area bounds;

    public Location getLocation() {
        return location;
    }

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

    public String getLocationType() {
        return locationType;
    }

    public void setLocationType(String locationType) {
        this.locationType = locationType;
    }

    public Area getViewport() {
        return viewport;
    }

    public void setViewport(Area viewport) {
        this.viewport = viewport;
    }

    public Area getBounds() {
        return bounds;
    }

    public void setBounds(Area bounds) {
        this.bounds = bounds;
    }
}


public class Location {
    private double lat;
    private double lng;

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLat() {
        return lat;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public double getLng() {
        return lng;
    }
}


public class Area {
    private Location southWest;
    private Location northEast;

    public Location getSouthWest() {
        return southWest;
    }

    public void setSouthWest(Location southWest) {
        this.southWest = southWest;
    }

    public Location getNorthEast() {
        return northEast;
    }

    public void setNorthEast(Location northEast) {
        this.northEast = northEast;
    }
}
然后尝试以下代码:

@Service
public class RestService {
    private static final String URL = "http://maps.googleapis.com/maps/api/geocode/json?address={address}&sensor=false";

    @Autowired
    private RestTemplate restTemplate;

    public GeocodeResponse getMap(String address) {
        Map<String, String> vars = new HashMap<String, String>();
        vars.put("address", address);

        String json = restTemplate.getForObject(URL,String.class, vars);

        return new Gson().fromJson(json, GeocodeResponse.class);
    }

}

希望有帮助。

完美答案。为我节省了至少半个小时:真的很好,但是因为来自谷歌的json可能随时都会改变。我们有更好的解决办法吗?例如:AddressComponent.longName现在是long\u名称。你的代码不能再得到长名字了。完美答案。为我节省了至少半个小时:真的很好,但是因为来自谷歌的json可能随时都会改变。我们有更好的解决办法吗?例如:AddressComponent.longName现在是long\u名称。你的代码不能再得到longName了。