2016-02-29 35 views
0

我轉換爲json並回顯我想使用jquery進行檢索的php數組。回顯的json的格式如下:嘗試解析json數組時未返回undefined

[{"id":"1","title":"Test Event 1","description":"This is the first test event","location":"Acme Hall, Room 101","contact":"John Smith","url":"http:\/\/www.example.com","start":"2016-02-29 13:00:00","end":"2016-02-29 14:00:00"}, 
{"id":"2","title":"Test Event 2","description":"This is the second test event","location":"Venable Hall, Room 101","contact":"Jane Smith","url":"http:\/\/www.example.com","start":"2016-03-08 09:00:00","end":"2016-03-10 10:45:00"}, 
{"id":"3","title":"Test Event 3","description":"This is the third test event","location":"Sitterson Hall, Room 200","contact":"Jane Smith","url":"http:\/\/www.example.com","start":"2016-03-18 15:00:00","end":"2016-02-22 16:30:00"}] 

當我嘗試使用Ajax GET來解析並顯示JSON時,會返回'undefined'。我的Ajax代碼是:

$.ajax({ 
    type: 'GET', 
    url: 'get-events.php', 
    dataType: "json", 
    cache: false, 
    success: function (result) { 
     alert(result[start]); 
    }, 
}); 

UPDATE:

感謝您的答覆。我終於得到它使用排序

for (var key in result) { 
if (result.hasOwnProperty(key)) { 
    result[key].id 

    } 
    } 
+0

什麼是變量'start'? – epascarello

回答

1

你的結果是一個表,所以你必須指定索引:結果[指數]

然後,JSON是一個十「關鍵」:「值」}格式,其中鍵是一個字符串。在你的代碼中你使用了start,這是一個未定義的變量。

所以,你應該嘗試:

alert(result[0]["start"]); 
0

看來你不使用結果值的正確方法。結果是一個對象數組。如果你想訪問第一個元素的起始值,你必須使用result[0].start

要訪問所有值,您應該使用for循環遍歷數組。

for (var i = 0; i < result.lenght; i++) { 
    // result[i].start 
}