Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson fromJson parsing nested json

Tags:

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"
    }
        ]
}

Above is the JSON string.

The equivalent classes for the JSON object is as follows :

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;
}   
}
}

The code to convert JSON to Java using Gson is as follows :

CarPriceResult cpr;
List<CarData> cd;
cpr = gson.fromJson(inputLine, CarPriceResult.class);
cd = cpr.getCarData();

object "cd" returns null and hence NPE is thrown.

what am I doing wrong?

like image 299
user2867209 Avatar asked Jul 13 '26 13:07

user2867209


1 Answers

Problem is in inputLine.
This code works well

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

EDIT:
Also, your json does not correspond to class mapping
Json:

"data": [  

class CarPriceResult

private List<CarData> CarData;  

You should rename field to data or change key in json to CarData.
e.g.
Json:

"data": [  

class CarPriceResult

private List<CarData> data;
like image 78
Ilya Avatar answered Jul 17 '26 17:07

Ilya