-1
我使用Gson从网站解析JSON文件。我在Java方面很新,希望找到正确的方法,我应该这样做。创建使用Gson库的类
一切工作正常,但我有几个问题。由于我从网站获取这些Json文件,我无法控制,json文件中的某些值为空。使用这些方法的正确方法是什么?我已经有方法从我的类中获取值并更改为所需的类型。
isp_ornd = “104%或类似的东西”
bsp_ornd =如上。
win_time =“2米35S 990”
正如我说没有,我只是想找出使用GSON和Java这样做的正确方法的任何问题的IM。
public class ResultData {
private String isp_ornd;
private String bsp_ornd;
private String win_time;
private RunnerData[] runners;
public int getIspOrnd() {
if(isp_ornd != null){
isp_ornd = isp_ornd.replace("%", "");
isp_ornd = isp_ornd.replace(" ", "");
if(isp_ornd.equals(""))
isp_ornd = "0";
return Integer.parseInt(isp_ornd);
}
else
return 0;
}
public int getBspOrnd() {
if(bsp_ornd != null){
bsp_ornd = bsp_ornd.replace("%", "");
bsp_ornd = bsp_ornd.replace(" ", "");
if(bsp_ornd.equals(""))
bsp_ornd = "0";
return Integer.parseInt(bsp_ornd);
}
else
return 0;
}
public long getWinTime() {
long minutes = 0;
long seconds = 0;
long milliseconds = 0;
long totalTime = 0;
if(win_time != null){
win_time = win_time.replace("m ",":");
win_time = win_time.replace(".",":");
win_time = win_time.replace("s","");
win_time = win_time.replace(" ","");
String[] timeSplit = win_time.split(":");
if(timeSplit.length == 3){
minutes = Long.parseLong(timeSplit[0]);
seconds = Long.parseLong(timeSplit[1]);
milliseconds = Long.parseLong(timeSplit[2]);
totalTime = (minutes * 36000) + (seconds * 1000) + (milliseconds*10);
}
else
totalTime = 0;
}
else
totalTime = 0;
return totalTime;
}
public RunnerData[] getRunners() {
return runners;
}
public String toString(){
return getIspOrnd() + " " + getBspOrnd() + " " + getWinTime() + " " + win_time;
}
}