I am getting NullPointerException while trying to deserialize json. I am trying to deserialize only select JSON objects from https://api.covid19india.org/data.json
I have written code
import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class CovidData { List<Statewise> statewiseData; public List<Statewise> getStatewiseData() { return statewiseData; } public void setStatewiseData(List<Statewise> statewiseData) { this.statewiseData = statewiseData; } @Override public String toString() { return "CovidData [statewiseData=" + statewiseData + "]"; } }
Statewise.java
public class Statewise { @JsonProperty("active") String active; public String getActive() { return active; } public void setActive(String active) { this.active = active; } @Override public String toString() { return "Statewise [active=" + active + ", confirmed=" + confirmed + ", deaths=" + deaths + ", deltaconfirmed=" + deltaconfirmed + ", deltadeaths=" + deltadeaths + ", deltarecovered=" + deltarecovered + ", lastupdatedtime=" + lastupdatedtime + ", migratedother=" + migratedother + ", recovered=" + recovered + ", state=" + state + ", statecode=" + statecode + ", statenotes=" + statenotes + "]"; } }
calling method
@Test public void test1() { CovidData covidData= given() .when() .get("https://api.covid19india.org/data.json") .as(CovidData.class); List<Statewise> state=covidData.getStatewiseData(); for (Statewise s : state) System.out.println(s.getActive()); }
Please help
Advertisement
Answer
The JSON returned contains “statewise” property.
So to get it deserialized correctly you can either change the property List<Statewise> statewiseData
to List<Statewise> statewise
in your CovidData class or add JsonProperty to map it to “statewise”
@JsonProperty("statewise") List<Statewise> statewiseData;