tags:

views:

14

answers:

0

I am using Gson to parse json files from a website. I am quite new at Java and want to find out the correct way i should be doing this.

Everything is working fine but i have a few questions. Since i am getting these Json files from a website i have no control over, some of the values in the json file are null. What is the proper way to work with these? I have get methods to get the values from my class and change to the desired type.

isp_ornd = "104% or something similar to that"

bsp_ornd = as above.

win_time = "2m 35s 990"

As i said im not having any problems i just want to find out the correct way on using Gson and Java for doing this.

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

}