Processing time value from JSON string

Hi

I need help processing a time value I've retrieved from MySQL through a web service. I can have it as a string but when I tried processing it as a DateTime and TimeSpan I've had no luck.

PHP web service code:
$response = array();

$sql = "SELECT startTime, endTime FROM doctor_work_days WHERE doctorNo=".$doctorNo." AND dayOfWeek='".$selectedDay."' ORDER BY startTime ASC";
$result = $connection->query($sql);

if ($result->num_rows > 0) {

    while($row = mysqli_fetch_array($result)){

            $data['startTime'] = $row['startTime'];
            $data['endTime'] = $row['endTime'];
            $fetch_data[] = $data;
    }

    $response['doctorWorkList'] = $fetch_data;
    $response['success'] = 1;

}
else
{
$response['success'] = 0;
}

echo json_encode($response);

Result:
{"doctorWorkList":[{"startTime":"10:00:00","endTime":"16:00:00"},{"startTime":"18:00:00","endTime":"22:00:00"}],"success":1}

Objects I'm processing it with:
public class DoctorWork
{
public string startTime { get; set; }

    public string endTime { get; set; }

}

public class DoctorWorkList
{
    public ObservableCollection<DoctorWork> doctorWorkList { get; set; }

    public string success { get; set; }
}

Code getting the json string:

result = response.Content.ReadAsStringAsync().Result;

            doctorWorkTime = JsonConvert.DeserializeObject<DoctorWorkList>(result);

            if (doctorWorkTime.success == "1")
            {

                await this.DisplayAlert("hey", result.ToString() +" - "+ doctorWorkTime.doctorWorkList[0].startTime +" - "+ doctorWorkTime.doctorWorkList[1].endTime, "OK");

            }
            else if (doctorWorkTime.success == "0")
            {

                await this.DisplayAlert("Sorry", "No doctors are available on this date. Please select another.", "OK");

            }

I can display the values and they are correct but I can't use them. Sorry if this is a stupid issue.

Thanks.

Best Answer

Answers

  • HashemXamarinHashemXamarin Member ✭✭

    Oh boy this code is all over the place

  • GaetanFGaetanF USMember ✭✭✭

    Did you try to turn your DoctorWork strings properties into TimeSpans?

  • HashemXamarinHashemXamarin Member ✭✭

    Yes I tried setting startTime and endTime as DateTimes and TimeSpans. I couldn't even display them anymore. My guess is the issue has to do with formatting the time values or changing their TIME type in MySQL.

Sign In or Register to comment.