1

我正在使用 JQuery 来调用我的 WCF 服务。响应正文显示了我的 JSON 格式数据,但我不确定如何解析它。请参阅我的代码以了解我到目前为止所做的事情。

 $.ajax({
            url: "http://wks52025:82/WcfDataService.svc/GetNotes()?$format=json",
            type: "get",
            datatype: "json",
            success: function (data) {
                $.each(data, function(i, item) {
                    alert(data[i].Title);
                })
            }
        });

    });

这是我的 JSON

{
    "d": [
        {
            "__metadata": {
                "id": "http://wks52025:82/WcfDataService.svc/tblNotes(guid'93629a5f-2bb3-4190-b876-3d8a2997e76a')",
                "uri": "http://wks52025:82/WcfDataService.svc/tblNotes(guid'93629a5f-2bb3-4190-b876-3d8a2997e76a')",
                "type": "GenesisOnlineModel.tblNote"
            },
            "NotesID": "93629a5f-2bb3-4190-b876-3d8a2997e76a",
            "NotesTitle": "BSKYB",
            "NotesText": "new Director of Brand and Media ",
            "ParentID": 8879,
            "ContactID": 309,
            "JobID": 1000088150,
            "UserID": "8b0e303a-68aa-49a5-af95-d994e2bdd5ac",
            "GroupID": null,
            "RelatedType": "Advertiser Contact",
            "IsShared": true
        },
        {
            "__metadata": {
                "id": "http://wks52025:82/WcfDataService.svc/tblNotes(guid'0f21866b-4a5c-417f-afe1-70ffbd1ce1f3')",
                "uri": "http://wks52025:82/WcfDataService.svc/tblNotes(guid'0f21866b-4a5c-417f-afe1-70ffbd1ce1f3')",
                "type": "GenesisOnlineModel.tblNote"
            },
            "NotesID": "0f21866b-4a5c-417f-afe1-70ffbd1ce1f3",
            "NotesTitle": "BSKYB More",
            "NotesText": "Contacted all major contacts on this profile",
            "ParentID": 8879,
            "ContactID": null,
            "JobID": null,
            "UserID": "8b0e303a-68aa-49a5-af95-d994e2bdd5ac",
            "GroupID": null,
            "RelatedType": "Advertiser",
            "IsShared": true
        }
    ]
}

在我的 Success 函数代码块中,我的警报中未定义。任何帮助都会很棒。

4

2 回答 2

4

关闭!在您的成功块中,执行以下操作:

        success: function (data) {
            $.each(data.d, function(i, item) {
                alert(item.NotesTitle);
            })
        }

更新:实施@Johans 评论。

于 2013-05-13T13:49:47.080 回答
2

你在警觉alert(data[i].Title);。从您的 JSON 的外观来看,JSON 数组对象中的任何对象都没有Title属性,这就是您得到undefined. 我看到NotesTitle但没有Title。将其更改为:

success: function (data) {
     $.each(data.d, function(i, item) {
          alert(item.NotesTitle);
     })
}
于 2013-05-13T13:50:07.513 回答