与GSON一起使用json的Java进程

时间:2012-12-06 09:31:59

标签: java json gson

我有一个JSON响应:

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "3",
               "short_name" : "3",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "1033",
               "short_name" : "1033",
               "types" : []
            },
            {
               "long_name" : "K osmidomkům",
               "short_name" : "K osmidomkům",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Praha-Suchdol",
               "short_name" : "Praha-Suchdol",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Praha",
               "short_name" : "Praha",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Hlavní město Praha",
               "short_name" : "Hlavní město Praha",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Hlavní město Praha",
               "short_name" : "Hlavní město Praha",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Česká republika",
               "short_name" : "CZ",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "165 00",
               "short_name" : "165 00",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Praha 620",
               "short_name" : "Praha 620",
               "types" : [ "postal_town" ]
            }
         ],
         "formatted_address" : "K osmidomkům 1033/3, 165 00 Praha-Praha-Suchdol, Česká republika",
         "geometry" : {
            "location" : {
               "lat" : 50.13670170,
               "lng" : 14.36865280
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 50.13805068029150,
                  "lng" : 14.37000178029150
               },
               "southwest" : {
                  "lat" : 50.13535271970850,
                  "lng" : 14.36730381970850
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

现在我需要通过GSON处理JSON,但我只需要来自“geometry”的部分。我需要位置(lat,lng)。如何将这片json绑定到POJO?我的意思是那样的

public class GoogleMapLocation
{
    @SerializedName("lat")
    private double latitude;

    @SerializedName("lng")
    private double longitude;

    public double getLatitude()
    {
        return latitude;
    }

    public void setLatitude(final double latitude)
    {
        this.latitude = latitude;
    }

    public double getLongitude()
    {
        return longitude;
    }

    public void setLongitude(final double longitude)
    {
        this.longitude = longitude;
    }
}

然后

GoogleMapLocation gml = gson.fromJson(jsonReader, GoogleMapLocation.class);

2 个答案:

答案 0 :(得分:2)

这应该有用(getter,setter,...为清晰起见省略,不防错):

public class GoogleMapLocation {
    List<Result> results;

    String status;
}

public class Result {
    @SerializedName("address_components")
    private List<AddressComponent> addressComponents;

    @SerializedName("formatted_address")
    private String formattedAddress;

    private Geometry geometry;

    @SerializedName("partial_match")
    private String partialMatch; // not sure of the type

    private List<String> types;
}

public class AddressComponents {
    @SerializedName("long_name")
    private String longName;

    @SerializedName("short_name")
    private String shortName;

    private List<String> types;
}

public class Geometry {
    LatLng location;

    Viewport viewport;

    Viewport bounds;

    @SerializedName("location_type")
    String locationType;
}

public class LatLng {
    @SerializedName("lat");
    private double latitude;

    @SerializedName("lng");
    private double longitude;
}

public class Viewport {
    private LatLng northeast;

    private LatLng southwest;
}

请参阅official documentation

编辑:请注意,某些字段也可以声明为枚举(status,...)

答案 1 :(得分:0)

采用RC的代码制作完整的工作样本

Test课程,以获取Geometry内容。

package com.yourcomp.test2;

import com.google.gson.Gson;

public class Test {


    public static void main(String[] args) {
        String test = "{" + 
        "   \"results\" : [" + 
        "      {" + 
        "         \"address_components\" : [" + 
        "            {" + 
        "               \"long_name\" : \"3\"," + 
        "               \"short_name\" : \"3\"," + 
        "               \"types\" : [ \"street_number\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"1033\"," + 
        "               \"short_name\" : \"1033\"," + 
        "               \"types\" : []" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"K osmidomkum\"," + 
        "               \"short_name\" : \"K osmidomkum\"," + 
        "               \"types\" : [ \"route\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"Praha-Suchdol\"," + 
        "               \"short_name\" : \"Praha-Suchdol\"," + 
        "               \"types\" : [ \"sublocality\", \"political\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"Praha\"," + 
        "               \"short_name\" : \"Praha\"," + 
        "               \"types\" : [ \"locality\", \"political\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"Hlavní mesto Praha\"," + 
        "               \"short_name\" : \"Hlavní mesto Praha\"," + 
        "               \"types\" : [ \"administrative_area_level_2\", \"political\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"Hlavní mesto Praha\"," + 
        "               \"short_name\" : \"Hlavní mesto Praha\"," + 
        "               \"types\" : [ \"administrative_area_level_1\", \"political\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"Ceská republika\"," + 
        "               \"short_name\" : \"CZ\"," + 
        "               \"types\" : [ \"country\", \"political\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"165 00\"," + 
        "               \"short_name\" : \"165 00\"," + 
        "               \"types\" : [ \"postal_code\" ]" + 
        "            }," + 
        "            {" + 
        "               \"long_name\" : \"Praha 620\"," + 
        "               \"short_name\" : \"Praha 620\"," + 
        "               \"types\" : [ \"postal_town\" ]" + 
        "            }" + 
        "         ]," + 
        "         \"formatted_address\" : \"K osmidomkum 1033/3, 165 00 Praha-Praha-Suchdol, Ceská republika\"," + 
        "         \"geometry\" : {" + 
        "            \"location\" : {" + 
        "               \"lat\" : 50.13670170," + 
        "               \"lng\" : 14.36865280" + 
        "            }," + 
        "            \"location_type\" : \"ROOFTOP\"," + 
        "            \"viewport\" : {" + 
        "               \"northeast\" : {" + 
        "                  \"lat\" : 50.13805068029150," + 
        "                  \"lng\" : 14.37000178029150" + 
        "               }," + 
        "               \"southwest\" : {" + 
        "                  \"lat\" : 50.13535271970850," + 
        "                  \"lng\" : 14.36730381970850" + 
        "               }" + 
        "            }" + 
        "         }," + 
        "         \"types\" : [ \"street_address\" ]" + 
        "      }" + 
        "   ]," + 
        "   \"status\" : \"OK\"" + 
        "}";
        GoogleMapLocation res = new Gson().fromJson(test, GoogleMapLocation.class);
        System.out.println(res);

        Geometry geometry = res.getResults().get(0).getGeometry();
        System.out.println(geometry);
    }

}

GoogleMapLocation

package com.yourcomp.test2;

import java.util.List;

import com.google.gson.annotations.SerializedName;

public class GoogleMapLocation {
    List<Result> results;

    String status;

    /**
     * Gets the results.
     * 
     * @return <tt> the results.</tt>
     */
    public List<Result> getResults() {
        return results;
    }

    /**
     * Sets the results.
     * 
     * @param results
     *            <tt> the results to set.</tt>
     */
    public void setResults(List<Result> results) {
        this.results = results;
    }

    /**
     * Gets the status.
     * 
     * @return <tt> the status.</tt>
     */
    public String getStatus() {
        return status;
    }

    /**
     * Sets the status.
     * 
     * @param status
     *            <tt> the status to set.</tt>
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "GoogleMapLocation [results=" + results + ", status=" + status
                + "]";
    }

}

class Result {
    @SerializedName("address_components")
    private List<AddressComponents> addressComponents;

    /**
     * Gets the addressComponents.
     * 
     * @return <tt> the addressComponents.</tt>
     */
    public List<AddressComponents> getAddressComponents() {
        return addressComponents;
    }

    /**
     * Sets the addressComponents.
     * 
     * @param addressComponents
     *            <tt> the addressComponents to set.</tt>
     */
    public void setAddressComponents(List<AddressComponents> addressComponents) {
        this.addressComponents = addressComponents;
    }

    /**
     * Gets the formattedAddress.
     * 
     * @return <tt> the formattedAddress.</tt>
     */
    public String getFormattedAddress() {
        return formattedAddress;
    }

    /**
     * Sets the formattedAddress.
     * 
     * @param formattedAddress
     *            <tt> the formattedAddress to set.</tt>
     */
    public void setFormattedAddress(String formattedAddress) {
        this.formattedAddress = formattedAddress;
    }

    /**
     * Gets the geometry.
     * 
     * @return <tt> the geometry.</tt>
     */
    public Geometry getGeometry() {
        return geometry;
    }

    /**
     * Sets the geometry.
     * 
     * @param geometry
     *            <tt> the geometry to set.</tt>
     */
    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }

    /**
     * Gets the partialMatch.
     * 
     * @return <tt> the partialMatch.</tt>
     */
    public String getPartialMatch() {
        return partialMatch;
    }

    /**
     * Sets the partialMatch.
     * 
     * @param partialMatch
     *            <tt> the partialMatch to set.</tt>
     */
    public void setPartialMatch(String partialMatch) {
        this.partialMatch = partialMatch;
    }

    /**
     * Gets the types.
     * 
     * @return <tt> the types.</tt>
     */
    public List<String> getTypes() {
        return types;
    }

    /**
     * Sets the types.
     * 
     * @param types
     *            <tt> the types to set.</tt>
     */
    public void setTypes(List<String> types) {
        this.types = types;
    }

    @SerializedName("formatted_address")
    private String formattedAddress;

    private Geometry geometry;

    @SerializedName("partial_match")
    private String partialMatch; // not sure of the type

    private List<String> types;

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Result [addressComponents=" + addressComponents
                + ", formattedAddress=" + formattedAddress + ", geometry="
                + geometry + ", partialMatch=" + partialMatch + ", types="
                + types + "]";
    }
}

class AddressComponents {
    private String long_name;
    private String short_name;
    private List<String> types;

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "AddressComponents [long_name=" + long_name + ", short_name="
                + short_name + ", types=" + types + "]";
    }

    /**
     * Gets the long_name.
     * 
     * @return <tt> the long_name.</tt>
     */
    public String getLong_name() {
        return long_name;
    }

    /**
     * Sets the long_name.
     * 
     * @param long_name
     *            <tt> the long_name to set.</tt>
     */
    public void setLong_name(String long_name) {
        this.long_name = long_name;
    }

    /**
     * Gets the short_name.
     * 
     * @return <tt> the short_name.</tt>
     */
    public String getShort_name() {
        return short_name;
    }

    /**
     * Sets the short_name.
     * 
     * @param short_name
     *            <tt> the short_name to set.</tt>
     */
    public void setShort_name(String short_name) {
        this.short_name = short_name;
    }

    /**
     * Gets the types.
     * 
     * @return <tt> the types.</tt>
     */
    public List<String> getTypes() {
        return types;
    }

    /**
     * Sets the types.
     * 
     * @param types
     *            <tt> the types to set.</tt>
     */
    public void setTypes(List<String> types) {
        this.types = types;
    }

}

class Geometry {
    LatLng location;

    Viewport viewport;

    Viewport bounds;

    @SerializedName("location_type")
    String locationType;

    /**
     * Gets the location.
     * 
     * @return <tt> the location.</tt>
     */
    public LatLng getLocation() {
        return location;
    }

    /**
     * Sets the location.
     * 
     * @param location
     *            <tt> the location to set.</tt>
     */
    public void setLocation(LatLng location) {
        this.location = location;
    }

    /**
     * Gets the viewport.
     * 
     * @return <tt> the viewport.</tt>
     */
    public Viewport getViewport() {
        return viewport;
    }

    /**
     * Sets the viewport.
     * 
     * @param viewport
     *            <tt> the viewport to set.</tt>
     */
    public void setViewport(Viewport viewport) {
        this.viewport = viewport;
    }

    /**
     * Gets the bounds.
     * 
     * @return <tt> the bounds.</tt>
     */
    public Viewport getBounds() {
        return bounds;
    }

    /**
     * Sets the bounds.
     * 
     * @param bounds
     *            <tt> the bounds to set.</tt>
     */
    public void setBounds(Viewport bounds) {
        this.bounds = bounds;
    }

    /**
     * Gets the locationType.
     * 
     * @return <tt> the locationType.</tt>
     */
    public String getLocationType() {
        return locationType;
    }

    /**
     * Sets the locationType.
     * 
     * @param locationType
     *            <tt> the locationType to set.</tt>
     */
    public void setLocationType(String locationType) {
        this.locationType = locationType;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Geometry [location=" + location + ", viewport=" + viewport
                + ", bounds=" + bounds + ", locationType=" + locationType + "]";
    }

}

class LatLng {
    @SerializedName("lat")
    private double latitude;

    @SerializedName("lng")
    private double longitude;

    /**
     * Gets the latitude.
     * 
     * @return <tt> the latitude.</tt>
     */
    public double getLatitude() {
        return latitude;
    }

    /**
     * Sets the latitude.
     * 
     * @param latitude
     *            <tt> the latitude to set.</tt>
     */
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    /**
     * Gets the longitude.
     * 
     * @return <tt> the longitude.</tt>
     */
    public double getLongitude() {
        return longitude;
    }

    /**
     * Sets the longitude.
     * 
     * @param longitude
     *            <tt> the longitude to set.</tt>
     */
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "LatLng [latitude=" + latitude + ", longitude=" + longitude
                + "]";
    }

}

class Viewport {
    private LatLng northeast;

    /**
     * Gets the northeast.
     * 
     * @return <tt> the northeast.</tt>
     */
    public LatLng getNortheast() {
        return northeast;
    }

    /**
     * Sets the northeast.
     * 
     * @param northeast
     *            <tt> the northeast to set.</tt>
     */
    public void setNortheast(LatLng northeast) {
        this.northeast = northeast;
    }

    /**
     * Gets the southwest.
     * 
     * @return <tt> the southwest.</tt>
     */
    public LatLng getSouthwest() {
        return southwest;
    }

    /**
     * Sets the southwest.
     * 
     * @param southwest
     *            <tt> the southwest to set.</tt>
     */
    public void setSouthwest(LatLng southwest) {
        this.southwest = southwest;
    }

    private LatLng southwest;

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Viewport [northeast=" + northeast + ", southwest=" + southwest
                + "]";
    }

}