{
"status_code": 200,
"status_text": "Success",
"count": 96,
"data": [
{
"model": "Spark",
"fuel_type": "",
"price": "352109",
"state": "",
"city": "Agartala",
"crawl_date": "2013-10-02 05:00:02",
"make": "Chevrolet",
"variant": "New Spark 1.0 BS4 OBDII"
},
{
"model": "Spark",
"fuel_type": "",
"price": "378914",
"state": "",
"city": "Agartala",
"crawl_date": "2013-10-02 05:00:03",
"make": "Chevrolet",
"variant": "New Spark 1.0 LS BS4 OBDII"
}
]
}
上面是 JSON 字符串。
JSON 对象的等效类如下:
public class CarPriceResult {
private int status_code;
private String status_text;
private int count;
private List<CarData> CarData;
public CarPriceResult() {
super();
}
public int getStatus_code() {
return status_code;
}
public void setStatus_code(int status_code) {
this.status_code = status_code;
}
public String getStatus_text() {
return status_text;
}
public void setStatus_text(String status_text) {
this.status_text = status_text;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<CarData> getCarData() {
return this.CarData;
}
public void setCarData(List<CarData> carData) {
this.CarData = carData;
}
public static class CarData {
private String model;
private String fuel_type;
private String state;
private String city;
private String crawl_date;
private String make;
private String variant;
public String getModel() {
return model;
}
public CarData() {
super();
}
public void setModel(String model) {
this.model = model;
}
public String getFuel_type() {
return fuel_type;
}
public void setFuel_type(String fuel_type) {
this.fuel_type = fuel_type;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCrawl_date() {
return crawl_date;
}
public void setCrawl_date(String crawl_date) {
this.crawl_date = crawl_date;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getVariant() {
return variant;
}
public void setVariant(String variant) {
this.variant = variant;
}
}
}
使用 Gson 将 JSON 转换为 Java 的代码如下:
CarPriceResult cpr;
List<CarData> cd;
cpr = gson.fromJson(inputLine, CarPriceResult.class);
cd = cpr.getCarData();
对象“cd”返回 null,因此抛出 NPE。
我究竟做错了什么?