Gson を使用して、Web サイトから json ファイルを解析しています。私はJavaが初めてで、これを行うべき正しい方法を見つけたいと思っています。
すべて正常に動作していますが、いくつか質問があります。私は制御できない Web サイトからこれらの Json ファイルを取得しているため、json ファイルの値の一部が null になっています。これらを扱う適切な方法は何ですか?クラスから値を取得し、目的の型に変更する get メソッドがあります。
isp_ornd = "104% かそれに近い"
bsp_ornd = 上記のとおり。
win_time = "2分35秒990"
私が言ったように、問題はありません。これを行うためにGsonとJavaを使用する正しい方法を見つけたいだけです。
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;
}
}