Android JSON数据类型变换的应对策略
程序员文章站
2024-01-31 08:26:52
...
{
"directionStatName": "猴子石大桥",
"lineName": "63路",
"range": 440,
"remainingTime": 0.88,
"distanceStion": 1,
"nextStation": "南郊公园"
}
以上述Json数据为例,这是一段实时公交的数据,所以会出现当前无实时信息的情况,如下:
{
"directionStatName": "猴子石大桥",
"lineName": "63路",
"range": "",
"remainingTime": "",
"distanceStion": "",
"nextStation": "南郊公园"
}
这个时候就发现,原本double、int类型的字段变成了String类型,这样在解析的时候就会出现闪退的问题。
一、原生JSON解析
import org.json.JSONException;
import org.json.JSONObject;
public class BusData {
private String directionStatName;
private String lineName;
private int range;
private double remainingTime;
private int distanceStion;
private String nextStation;
public BusData(String jsonStr) {
try {
JSONObject info = new JSONObject(jsonStr);
try {
this.directionStatName = info.getString("directionStatName");
} catch (Exception e) {
this.directionStatName = "";
}
try {
this.lineName = info.getString("lineName");
} catch (Exception e) {
this.lineName = "";
}
try {
this.range = info.getInt("range");
} catch (Exception e) {
this.range = -1;
}
try {
this.remainingTime = info.getInt("remainingTime");
} catch (Exception e) {
this.remainingTime = 0.0;
}
try {
this.distanceStion = info.getInt("distanceStion");
} catch (Exception e) {
this.distanceStion = -1;
}
try {
this.nextStation = info.getString("nextStation");
} catch (Exception e) {
this.nextStation = "";
}
}catch (JSONException e) {
e.printStackTrace();
}
}
public String getDirectionStatName() {
return directionStatName;
}
public void setDirectionStatName(String directionStatName) {
this.directionStatName = directionStatName;
}
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public int getRange() {
return range;
}
public void setRange(int range) {
this.range = range;
}
public double getRemainingTime() {
return remainingTime;
}
public void setRemainingTime(double remainingTime) {
this.remainingTime = remainingTime;
}
public int getDistanceStion() {
return distanceStion;
}
public void setDistanceStion(int distanceStion) {
this.distanceStion = distanceStion;
}
public String getNextStation() {
return nextStation;
}
public void setNextStation(String nextStation) {
this.nextStation = nextStation;
}
}
我最开始写代码的时候就是封装的原生解析的方法,这样可以有效避免程序闪退。然后在取值的时候根据返回值做判断就行了。这段代码这次项目没有使用,不确定是否正确,这次我用的GSON解析。
二、GSON解析
因为数据类型的改变,所以对于存在改变隐患的字段采用Object的类型取值,GSON解析的基类:
public class BusData {
/**
* directionStatName : 猴子石大桥
* lineName : 63路
* range : 440
* remainingTime : 0.88
* distanceStion : 1
* nextStation : 南郊公园
*/
private String directionStatName;
private String lineName;
private Object range;
private Object remainingTime;
private Object distanceStion;
private String nextStation;
public String getDirectionStatName() {
return directionStatName;
}
public void setDirectionStatName(String directionStatName) {
this.directionStatName = directionStatName;
}
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public Object getRange() {
return range;
}
public void setRange(int range) {
this.range = range;
}
public Object getRemainingTime() {
return remainingTime;
}
public void setRemainingTime(double remainingTime) {
this.remainingTime = remainingTime;
}
public Object getDistanceStion() {
return distanceStion;
}
public void setDistanceStion(int distanceStion) {
this.distanceStion = distanceStion;
}
public String getNextStation() {
return nextStation;
}
public void setNextStation(String nextStation) {
this.nextStation = nextStation;
}
}
在取值的时候,用instanceof判断返回的数据类型,再根据类型做相应处理
Object remainTime = list.get(position).getRemainingTime();
if (remainTime instanceof Double) {
double time = ((Double) remainTime).doubleValue();
} else {
// 相应处理
}
double range;
if (list.get(position).getRange() instanceof Integer) {
range = (int) list.get(position).getRange() + 0.0;
} else {
range = ((Double) list.get(position).getRange()).doubleValue();
}
差不多就酱。