Mapping json to multiple Pojo classes

Question

So I'm working on a project for a Java class and I need to fetch weather data. I'm using the weather underground api. I haven't really learned yet how to do APIs so I'm self taught. I've gotten a location API working by using JsonSchemaToPojo to create a class and then using Jackson and the following code snippet.

    userLocation= new ObjectMapper().readValue(json, Location.class);

This properly fills out the Location object. For the weather API JsonSchemaToPojo gave me multiple classes. The Json that it returns looks like this.

  "response": {
      "version":"0.1",
      "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
      "features": {
      "conditions": 1
      }
        }
      , "current_observation": {
            "image": {
            "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
            "title":"Weather Underground",
            "link":"http://www.wunderground.com"
            },
            "display_location": {
            "full":"Tucson, AZ",
            "city":"Tucson",
            "state":"AZ",
            "state_name":"Arizona",
            "country":"US",
            "country_iso3166":"US",
            "zip":"85701",
            "magic":"1",
            "wmo":"99999",
            "latitude":"32.22000122",
            "longitude":"-110.97000122",
            "elevation":"728.2"
            },
            "observation_location": {
            "full":"Feldmans, Tucson, Arizona",
            "city":"Feldmans, Tucson",
            "state":"Arizona",
            "country":"US",
            "country_iso3166":"US",
            "latitude":"32.241463",
            "longitude":"-110.970146",
            "elevation":"2389 ft"
            },
            "estimated": {
            },
            "station_id":"KAZTUCSO658",
            "observation_time":"Last Updated on November 29, 10:39 AM MST",
            "observation_time_rfc822":"Wed, 29 Nov 2017 10:39:41 -0700",
            "observation_epoch":"1511977181",
            "local_time_rfc822":"Wed, 29 Nov 2017 10:39:48 -0700",
            "local_epoch":"1511977188",
            "local_tz_short":"MST",
            "local_tz_long":"America/Phoenix",
            "local_tz_offset":"-0700",
            "weather":"Partly Cloudy",
            "temperature_string":"70.9 F (21.6 C)",
            "temp_f":70.9,
            "temp_c":21.6,
            "relative_humidity":"25%",
            "wind_string":"Calm",
            "wind_dir":"SSE",
            "wind_degrees":165,
            "wind_mph":0.0,
            "wind_gust_mph":0,
            "wind_kph":0,
            "wind_gust_kph":0,
            "pressure_mb":"1017",
            "pressure_in":"30.04",
            "pressure_trend":"0",
            "dewpoint_string":"33 F (1 C)",
            "dewpoint_f":33,
            "dewpoint_c":1,
            "heat_index_string":"NA",
            "heat_index_f":"NA",
            "heat_index_c":"NA",
            "windchill_string":"NA",
            "windchill_f":"NA",
            "windchill_c":"NA",
            "feelslike_string":"70.9 F (21.6 C)",
            "feelslike_f":"70.9",
            "feelslike_c":"21.6",
            "visibility_mi":"10.0",
            "visibility_km":"16.1",
            "solarradiation":"238",
            "UV":"2.0","precip_1hr_string":"0.00 in ( 0 mm)",
            "precip_1hr_in":"0.00",
            "precip_1hr_metric":" 0",
            "precip_today_string":"0.00 in (0 mm)",
            "precip_today_in":"0.00",
            "precip_today_metric":"0",
            "icon":"partlycloudy",
            "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
            "forecast_url":"http://www.wunderground.com/US/AZ/Tucson.html",
    "history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?
        ID=KAZTUCSO658","ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=32.241463,-110.970146",
            "nowcast":""
    }
}

My Java classes generated look similar to this

-----------------------------------hydraplant.primary.Response.java-----------------------------------
    package hydraplant.primary; 
    import java.util.HashMap;
    import java.util.Map;
    import com.fasterxml.jackson.annotation.JsonAnyGetter;
    import com.fasterxml.jackson.annotation.JsonAnySetter;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
    "version",
    "termsofService",
    "features"
    })
    public class Response {

    @JsonProperty("version")
    private String version;
    @JsonProperty("termsofService")
    private String termsofService;
    @JsonProperty("features")
    private Features features;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("version")
    public String getVersion() {
    return version;
    }

    @JsonProperty("version")
    public void setVersion(String version) {
    this.version = version;
    }

    @JsonProperty("termsofService")
    public String getTermsofService() {
    return termsofService;
    }

    @JsonProperty("termsofService")
    public void setTermsofService(String termsofService) {
    this.termsofService = termsofService;
    }

    @JsonProperty("features")
    public Features getFeatures() {
    return features;
    }

    @JsonProperty("features")
    public void setFeatures(Features features) {
    this.features = features;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }

    }
    -----------------------------------hydraplant.primary.Weather.java-----------------------------------

    package hydraplant.primary;

    import java.util.HashMap;
    import java.util.Map;
    import com.fasterxml.jackson.annotation.JsonAnyGetter;
    import com.fasterxml.jackson.annotation.JsonAnySetter;
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonPropertyOrder;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
    "response",
    "current_observation"
    })
    public class Weather {

    @JsonProperty("response")
    private Response response;
    @JsonProperty("current_observation")
    private CurrentObservation currentObservation;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("response")
    public Response getResponse() {
    return response;
    }

    @JsonProperty("response")
    public void setResponse(Response response) {
    this.response = response;
    }

    @JsonProperty("current_observation")
    public CurrentObservation getCurrentObservation() {
    return currentObservation;
    }

    @JsonProperty("current_observation")
    public void setCurrentObservation(CurrentObservation currentObservation) {
    this.currentObservation = currentObservation;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }}</code>

This is only 2 of the 9 classes it generated as the code is very long and would detract from my question. But what i'm trying to do is map the JSON response to all of the different classes similarly to what I did for the location object. I tried to just repeat the ObjectMapper line with each different class as the second argument but it gives me an error. My apologies in advance because this is probably a conceptual error with me not really understanding what the code is doing but I searched around for a few hours but couldn't find the answer. Any help with this problem would be appreciated.I'd like to be able to understand this but it doesn't look like we're going to be taught anything like this anytime soon.


Show source
| java   | json   | jackson   | class   2017-11-29 21:11 0 Answers

Answers to Mapping json to multiple Pojo classes ( 0 )

Leave a reply to - Mapping json to multiple Pojo classes

◀ Go back