2012-12-05 99 views
0

我得到一個JSON字符串如下無法讀取JSON字符串

[ 
    { 
     "id": 20, 
     "title": "a", 
     "allDay": true, 
     "start": "2012-12-04 00:00", 
     "end": "2012-12-07 00:00", 
     "color": "#eb491d", 
     "startDate": "/Date(1354559400000)/", 
     "endDate": "/Date(1354818600000)/", 
     "Location": "a", 
     "StartDatestr": "2012-12-04", 
     "EndDatestr": "2012-12-04", 
     "StartTime": "00:00", 
     "EndTime": "00:00", 
     "Alert": false, 
     "Repeat": false, 
     "RepeatDays": 0, 
     "CalendarID": 13, 
     "CustomerNo": 593963, 
     "CustomerName": "test_apple", 
     "IsProspect": true, 
     "Description": "" 
    } 
] 

當我檢查長度如下返回「1」

console.log(myAppointment.length); 

但是當我嘗試爲訪問在它下面給出未定義的錯誤。

var myAppointment = JSON.parse(data.d); 
sessionStorage.CustomerID = data.CustomerNo; 
console.log(sessionStorage.CustomerID); 

我不知道。誰能告訴我什麼,我在這裏失蹤

+1

您的JSON數據似乎包裹在一個陣列。你可以在你的代碼中嘗試myAppointment [0]。 – ryadavilli

+2

data.CustomerNo是未定義的,因爲它應該是myAppointment.CustomerNo。那麼,這是一個數組還是不是?那麼它應該是myAppointment [0] .CustomerNo。 –

+1

'console.log(myAppointment.id);'不給你myAppointment的長度,它給你預約的ID。 –

回答

2

更改您解析代碼:

var myAppointment = JSON.parse(data.d)[0]; 
0

變化的第二行象下面這樣:

var myAppointment = JSON.parse(data.d); 
sessionStorage.CustomerID = myAppointment.CustomerNo;// from data.CustomerNo 
console.log(sessionStorage.CustomerID); 

你試圖從數據對象訪問解析片。