Gson fromJson解析嵌套的json

时间:2013-10-10 13:41:05

标签: json gson

{
"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();

object“cd”返回null,因此抛出NPE。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

问题出在inputLine 这段代码效果很好

String str = "{ \"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\" } ] }";
CarPriceResult cpr = new Gson().fromJson(str, CarPriceResult.class);
List<CarData> cd = cpr.getCarData();  

修改
此外,您的json与类映射不对应 Json:

"data": [  

类CarPriceResult

private List<CarData> CarData;  

您应该将字段重命名为data或将json中的密钥更改为CarData e.g。
Json:

"data": [  

类CarPriceResult

private List<CarData> data;