gpt4 book ai didi

json - Gson fromJson 解析嵌套json

转载 作者:行者123 更新时间:2023-12-02 03:42:20 27 4
gpt4 key购买 nike

{
"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。

我做错了什么?

最佳答案

问题出在 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
例如
JSON:

"data": [  

CarPriceResult 类

private List<CarData> data;

关于json - Gson fromJson 解析嵌套json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19297425/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com