2017-03-19 35 views
3

我正在使用商品請求爲使用其學生ID的學生檢索出勤信息。從sql查詢生成完整日曆的json事件

<Request method="GET" item="true"> 
    <Query> 
    select subject, in_time, out_time 
    from tbl_attendance where student_id = $id 
    </Query> 
</Request> 

目前,我正在以這種請求的以下格式獲取json。

https://api.metamug.com/tinkertech/v1.0/attendance/1000

[ 
    { 
     "out_time": "2017-03-18 12:00:01.0", 
     "in_time": "2017-03-18 12:00:01.0", 
     "subject": "Maths" 
    }, 
    { 
     "out_time": "2017-03-19 13:00:01.0", 
     "in_time": "2017-03-18 12:00:01.0", 
     "subject": "Bio" 
    } 

] 

我試圖生成事件JSON按照這個答案 Jquery Full Calendar json event source syntax

回答

2

改變你的資源XML中的SQL如下:

<Query> 
     SELECT subject AS title, DATE_FORMAT(in_time,'%Y-%m-%dT%T') AS start, 
      DATE_FORMAT(out_time,'%Y-%m-%dT%T') AS end 
      FROM tbl_attendance WHERE student_id=$id 
    </Query> 

注意, 'AS'關鍵字用於在響應中命名JSON密鑰。這應該將您的上述JSON響應轉換爲如下形式:

[ 
    { 
     "end": "2017-03-18 12:00:01", 
     "start": "2017-03-18 12:00:01", 
     "title": "Maths" 
    }, 
    { 
     "end": "2017-03-19 13:00:01", 
     "start": "2017-03-18 12:00:01", 
     "title": "Bio" 
    } 
] 
+0

我在json中獲得T,但它現在可行。例如「2017-03-19T13:00:01」 – Sorter

+0

這非常有幫助。未在文檔中提到https://metamug.com/docs/ – Sorter