Announcement Announcement Module
Collapse
No announcement yet.
Parsing api result: JsonMappingException: Unrecognized field "address_components" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parsing api result: JsonMappingException: Unrecognized field "address_components"

    Hello everyone!
    I am trying to to create an object with Jackson 1.9.9 from a google geocoder api response.
    (taken from google, only the structure is important, mine looks the same, but longer)
    Code:
    {
      "status": "OK",
      "results": [ {
        "types": street_address,
        "formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
        "address_components": [ {
          "long_name": "275-291",
          "short_name": "275-291",
          "types": street_number
        }, {
          "long_name": "Bedford Ave",
          "short_name": "Bedford Ave",
          "types": route
        }, {
          "long_name": "New York",
          "short_name": "New York",
          "types": [ "locality", "political" ]
        }, {
          "long_name": "Brooklyn",
          "short_name": "Brooklyn",
          "types": [ "administrative_area_level_3", "political" ]
        }, {
          "long_name": "Kings",
          "short_name": "Kings",
          "types": [ "administrative_area_level_2", "political" ]
        }, {
          "long_name": "New York",
          "short_name": "NY",
          "types": [ "administrative_area_level_1", "political" ]
        }, {
          "long_name": "United States",
          "short_name": "US",
          "types": [ "country", "political" ]
        }, {
          "long_name": "11211",
          "short_name": "11211",
          "types": postal_code
        } ],
        "geometry": {
          "location": {
            "lat": 40.7142298,
            "lng": -73.9614669
          },
          "location_type": "RANGE_INTERPOLATED",
          "viewport": {
            "southwest": {
              "lat": 40.7110822,
              "lng": -73.9646145
            },
            "northeast": {
              "lat": 40.7173774,
              "lng": -73.9583193
            }
          }
        }
      },
      ...
    The corresponding class:

    Code:
    // @JsonIgnoreProperties(ignoreUnknown = true)
    public class GeoCodeResponse
    {
    	@JsonProperty("status")
    	public String status;
    
    	@JsonProperty("results")
    	public results[] results;
    
    	public GeoCodeResponse() {
    	}
    
    	public class results
    	{
    		@JsonCreator
    		public results() {
    		}
    		
    		@JsonProperty("address_components")
    		public address_components[] address_components;
    
    		@JsonProperty("formatted_address")
    		public String formatted_address;	
    	}
    
    	public class address_components
    	{
    		@JsonCreator
    		public address_components() {
    		}
    		
    		@JsonProperty("long_name")
    		public String long_name;
    		
    		@JsonProperty("short_name")
    		public String short_name;
    		
    		@JsonProperty("types")
    		public String[] types;
    	}
    }
    my service call:
    Code:
    final String apiResponse = getGeoCoderApiResponse(latitude, longitude);
       LOG.debug("api response: {}", apiResponse);
       try {
    	final GeoCodeResponse response = objectMapper.readValue(apiResponse, GeoCodeResponse.class);
    	LOG.debug("parsed GeoCodeResponse: {}", response);
       }
    }
    It always results in a JsonMappingException: Unrecognized field "address_components".
    I don't understand why that happens!
    When I use @JsonIgnoreProperties(ignoreUnknown = true), the fields address_components and formatted_date in the mapping class are always NULL, although I think, that the modelling of the GeoCodeResponse is correct...

    On the other side, results und status are set with correct values...

    Thanks in advance for any idea!!!
Working...
X