【问题标题】:Parsing this JSON using JQuery使用 JQuery 解析这个 JSON
【发布时间】:2013-05-13 13:47:27
【问题描述】:

我正在使用 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 函数代码块中,我的警报中未定义。任何帮助都会很棒。

【问题讨论】:

  • 尝试将整个 data 变量打印到控制台。一种想法是,由于“标题”键不存在,所以这是问题而不是解析。
  • 我正在使用 Chrome 的 Web 开发人员工具来查看网络请求和响应。它返回 JSON 的主体。我还可以键入服务 URL 来查看 JSON。

标签: jquery ajax json parsing


【解决方案1】:

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

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

更新:实现@Johans 评论。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 感谢这工作。将数据更改为 data.d 是我所缺少的。我知道这是我想念的小东西:(
    • 我有,你的 JQuery 能力对 SO 来说太快了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多