1

I have the following code with json, that I got from accuweather

{  
 "Headline":{  
      "EffectiveDate":"2019-07-29T07:00:00+06:00",
      "EffectiveEpochDate":1564362000,
      "Severity":3,
      "Text":"The heat wave will continue through Friday",
      "Category":"heat",
      "EndDate":"2019-08-02T19:00:00+06:00",
      "EndEpochDate":1564750800
   },
   "DailyForecasts":[  
      {  
         "Date":"2019-07-29T07:00:00+06:00",
         "EpochDate":1564362000,
         "Temperature":{  
            "Minimum":{  
               "Value":19.1,
               "Unit":"C",
               "UnitType":17
            },
            "Maximum":{  
               "Value":36.7,
               "Unit":"C",
               "UnitType":17
            }
         },
         "Day":{  
            "Icon":30,
            "IconPhrase":"Hot",
            "HasPrecipitation":false
         },
         "Night":{  
            "Icon":35,
            "IconPhrase":"Partly cloudy",
            "HasPrecipitation":false
         },
         "Sources":[  
            "AccuWeather"
         ]
      }
   ]
}

I try to parse this object to the POJO via jackson

public static void main( String[] args )
{
    String x = "{\"Headline\":{\"EffectiveDate\":\"2019-07-29T07:00:00+06:00\",\"EffectiveEpochDate\":1564362000,\"Severity\":3,\"Text\":\"The heat wave will continue through Friday\",\"Category\":\"heat\",\"EndDate\":\"2019-08-02T19:00:00+06:00\",\"EndEpochDate\":1564750800},\"DailyForecasts\":[{\"Date\":\"2019-07-29T07:00:00+06:00\",\"EpochDate\":1564362000,\"Temperature\":{\"Minimum\":{\"Value\":19.1,\"Unit\":\"C\",\"UnitType\":17},\"Maximum\":{\"Value\":36.7,\"Unit\":\"C\",\"UnitType\":17}},\"Day\":{\"Icon\":30,\"IconPhrase\":\"Hot\",\"HasPrecipitation\":false},\"Night\":{\"Icon\":35,\"IconPhrase\":\"Partly cloudy\",\"HasPrecipitation\":false},\"Sources\":[\"AccuWeather\"]}]}";
    ObjectMapper objectMapper = new ObjectMapper();

    try {
        Weather weather = objectMapper.readValue(x, Weather.class);

        System.out.println(weather);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I have all the models specified in the json like Headline, array of DailyForecasts, Temperature that consists of TemperatureItems(named minimum and maximum as in json) and etc, all of them have private fields and public constructor, getters and setters. However I do not have some of the fields as I want to omit them(Day, Night, EpochDate, Source).

When I run the program I get the error

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of test.models.weather.Weather (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

I have also tried Gson but it return object with Null values

Am I doing something wrong? Is there another way to do it?

Edit: These are the models, @LazerBass was right, as I firstly didn't include default constructors, now the error has changed:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Headline" (class test.models.weather.Weather), not marked as ignorable (2 known properties: "headline", "dailyForecasts"])

public class TemperatureItem {
    public double value;
    public String unit;
    public String unitType;

    public TemperatureItem() {

    }

    //Getters and setters
}


public class Temperature {
    private TemperatureItem maximum;
    private TemperatureItem minimum;

    public Temperature(TemperatureItem maximum, TemperatureItem minimum) {
        this.maximum = maximum;
        this.minimum = minimum;
    }

    public Temperature() {

    }
    //Getters and setters
}

public class DailyForecasts {
    private LocalDateTime date;
    private Temperature temperature;

    public DailyForecasts(LocalDateTime date, Temperature temperature) {
        this.date = date;
        this.temperature = temperature;
    }

    public DailyForecasts() {

    }
    //Getters and setters
}

public class Headline {
    private LocalDateTime effectiveDate;
    private int severity;
    private String text;
    private String category;
    private LocalDateTime endDate;

    public Headline() {

    }

    public Headline(LocalDateTime effectiveDate, Integer severity, String text, String category, LocalDateTime endDate) {
        this.effectiveDate = effectiveDate;
        this.severity = severity;
        this.text = text;
        this.category = category;
        this.endDate = endDate;
    }
    //Getters and setters
}

public class Weather {
        private Headline headline;
        private DailyForecasts[] dailyForecasts;

        public Weather() {

        }
        public Weather(Headline headline, DailyForecasts[] dailyForecasts) {
            this.headline = headline;
            this.dailyForecasts = dailyForecasts;
        }
        //Getters and setters
}

I have found out, that if I convert json string to lowercase, I can get some values, although Array and LocalDateTime weren't parsed

8
  • not seeing the Weather definition, but try Weather weath = (Weather.class)JSON.deserializeStrict(x,Weather.class); Commented Jul 29, 2019 at 16:32
  • 1
    Would be better to add the models you defined. Commented Jul 29, 2019 at 16:35
  • You can use JsonIgnore or JsonIgnoreProperties to the model so that you can ignore those unwanted fields. Commented Jul 29, 2019 at 16:35
  • Your Weather class is incorrect. If you want help fixing it, edit the question and show us the source code for it, and its embedded classes (Headline, DailyForecast, Temperature, ...) Commented Jul 29, 2019 at 16:47
  • Sure, have updated the question now, thank you, @MadhuBhat Commented Jul 29, 2019 at 17:18

3 Answers 3

1

To generate the Weather classes and its corresponding classes, use the following link and select the source type as json. It will generate the required classes as per the json string.

http://www.jsonschema2pojo.org/

After generating the classes, you can annotate the fields with @JsonIgnore which are not required.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, using generated classes it deserialized correctly
1

When Jackson fails with message like "no Creators, like default construct, exist" it needs default, public no-argument constructor for each POJO class you have in your model.

When it fails with message like "Unrecognized field ... not marked as ignorable ..." you need to disable FAIL_ON_UNKNOWN_PROPERTIES feature.

See also:

  1. Jackson Unmarshalling JSON with Unknown Properties
  2. jackson delay deserializing field

Comments

1

Judging from the exception message I would guess that your Weather class is laking an no-argument constructor. Try adding one. E.g.

public class Weather {
    public Weather() {
        // no arg constructor needed for jackson
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.