无法读取JSON字符串

时间:2012-12-05 05:38:00

标签: javascript jquery json web-services

我正在获取一个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);

我不知道。任何人都可以告诉我我在这里缺少什么

2 个答案:

答案 0 :(得分:2)

将解析代码更改为:

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

答案 1 :(得分:0)

更改第二行,如下所示:

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

您试图从数据对象中访问已解析的片段。