【发布时间】:2021-07-01 04:19:42
【问题描述】:
我在尝试反序列化 json 时收到 NullPointerException。我正在尝试仅反序列化来自 https://api.covid19india.org/data.json
的选择 JSON 对象我写过代码
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 + "]";
}
}
调用方法
@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());
}
请帮忙
【问题讨论】:
-
能否提供异常的stacktrace。
-
java.lang.NullPointerException at RestAssured.com.restassured.DeserializationTest2.test1(DeserializationTest2.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
-
我可以看到,该 JSON 中没有
statewiseData,您正在for循环中对其进行迭代。这可能是问题的关键。 -
它就在那里,它是长 JSON