【问题标题】:Extract a specific item from json data从 json 数据中提取特定项目
【发布时间】:2013-11-20 18:10:13
【问题描述】:

我有一个返回类别列表的 ajax 调用。我希望从列表中提取每个名称。 我的 ajax 看起来像:

$(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "/Client/GetCategoryList",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function () { alert("Error"); }
            });
        });

如果我使用的是“dataType:”json”,那么在警报中我发现了类似的结果

[object Object],[object Object],[object Object],[object Object],[object Object]

如果我评论“dataType:”json”那么结果就像

[{"iID":56,"strName":"as","strDescription":"as","strImage":""},{"iID":24,"strName":"laptop","strDescription":"laptops","strImage":"uploads/2dell.jpg"}, {"iID":14,"strName":"mobile","strDescription":"handsets","strImage":"uploads/14sams.jpeg"},{"iID":46,"strName":"sds","strDescription":"dsd","strImage":"uploads/Category/46bg.jpg"}]

我只是想从数据中提取类别名称或 strName 作为超链接。任何帮助都将不胜感激。提前致谢。

【问题讨论】:

标签: jquery ajax json


【解决方案1】:
var names = $.map(data, function(v){
    return v.strName;
});

【讨论】:

  • @rns Np!我以为你可以用这个找出超链接部分;)
【解决方案2】:

由于您要返回一个数组,因此您需要对其进行循环:

    success: function (data) {
        $.each(data, function(i, el) {
            console.log(el.strName);
        });
    },

【讨论】:

    【解决方案3】:

    您可以在 ajax 调用的成功函数中使用data.strName 提取“strName”;

    抱歉,我没有看到它是一个数组。您可以尝试 getJson 方法,例如:

    $.getJSON(url, param ,function(data) { //url - return json page
       $.each(data, function() {
           alert(this.strName);
       });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-27
      • 2020-10-02
      • 2015-02-18
      • 2015-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多