为什么我会为此JSON数组中的每个项目保持“未定义”?

时间:2012-01-18 14:41:07

标签: jquery json

以下是将'undefined'作为每个项目的值返回。有人能告诉我为什么吗?

Jquery的

    $("button").click(function () {
    var estimateID = this.id;
    var baseURL = "/Dashboard/EstimateDetails/";
    var url = baseURL + estimateID;
    $.getJSON(url, function (data) {
        for (var i = 0; i <= data.details.length; i++) {
            $('#Details').html("<p>item1=" + data.details.Dma + " item2=" + data.details.Callsign + " item3=" + data.details.Description + "</p>");
        }
    });

JSON

{
    "details": [{
        "Dma": "Albany-Schenectady-Troy",
        "CallSign": "WRGB",
        "Description": "WRGB (CBS) Schenectady"
    }, {
        "Dma": "Albany-Schenectady-Troy",
        "CallSign": "WTEN",
        "Description": "WTEN (ABC) Albany "
    }, {
        "Dma": "Albany-Schenectady-Troy",
        "CallSign": "WXXA",
        "Description": "WXXA (Fox) Albany "
    }, {
        "Dma": "Atlanta",
        "CallSign": "WGCL",
        "Description": "WGCL (CBS) Atlanta "
    }, {
        "Dma": "Atlanta",
        "CallSign": "WXIA",
        "Description": "WXIA (NBC) Atlanta "
    }, {
        "Dma": "Austin",
        "CallSign": "KXAN",
        "Description": "KXAN (NBC) Austin "
    }, {
        "Dma": "Austin",
        "CallSign": "KVUE",
        "Description": "KVUE (ABC) Austin "
    }, {
        "Dma": "Baltimore",
        "CallSign": "WMAR",
        "Description": "WMAR (ABC) Baltimore "
    }, {
        "Dma": "Baltimore",
        "CallSign": "WBAL",
        "Description": "WBAL (NBC) Baltimore"
    }, {
        "Dma": "Baltimore",
        "CallSign": "WJZ ",
        "Description": "WJZ  (CBS) Baltimore "
    }, {
        "Dma": "Baltimore",
        "CallSign": "WBFF",
        "Description": "WBFF (Fox) Baltimore "
    }]
}

编辑 - 运行上述代码时的结果是添加到详细信息div的单行HTML,如下所示:

item1=undefined item2=undefined item3=undefined

6 个答案:

答案 0 :(得分:3)

data.details.Callsign错了

data.details[i].Callsign

是你想要的,也就是你循环details时缺少数组索引。请注意,您正在为多个变量执行此操作。

清洁代码可能有所帮助。像这样分离变量是一个好的开始......

 $.getJSON(url, function (data) {
     var details = data.details,
         dma, callSign,...; // and the rest of whatever you need

     for (var i = 0; i <= details.length; i++) {
         dma      = details[i].Dma;
         callSign = details[i].Callsign;
         // more here

         $('#Details').html("<p>item1=" + dma + " item2=" + callSign + ...
     }            
 });

更具可读性,更不容易出错恕我直言。

编辑 - 正如评论中所指出的,html每次都会在循环中覆盖div的内容。您可能需要append

答案 1 :(得分:1)

你在循环中省略了数组索引。

$("button").click(function () {
    var estimateID = this.id;
    var baseURL = "/Dashboard/EstimateDetails/";
    var url = baseURL + estimateID;
    $.getJSON(url, function (data) {
        for (var i = 0; i <= data.details.length; i++) {
            $('#Details').html("<p>item1=" + data.details[i].Dma + " item2=" + data.details[i].Callsign + " item3=" + data.details[i].Description + "</p>");
        }
    });

答案 2 :(得分:1)

您已将details定义为数组,因此需要对其进行索引以访问其成员。例如,在for循环中,您需要这种类型的东西:

data.details[i].Dma

而不是

data.details.Dma

顺便说一句,你可以使用jQuery每个回调而不是for循环:

if (data && data.details) {
    $.each(data.details, function() {
            $('#Details').html("<p>item1=" + this.Dma + " item2=" + this.Callsign +
                " item3=" + this.Description + "</p>");
        });
}

在传递给每个回调函数的回调函数中,this代表for循环中的data.details[i]

答案 3 :(得分:1)

使用

data.details[i].Dma

同样在循环-1中添加data.details.length-1,因为索引以0开头。

for (var i = 0; i <= data.details.length-1; i++) {
  $('#Details').html("<p>item1=" + data.details[i].Dma + " item2=" + data.details[i].Callsign + " item3=" + data.details[i].Description + "</p>");
}

演示:http://jsfiddle.net/aqPvL/

答案 4 :(得分:0)

在for循环中,您需要通过data.details[i]

引用您的对象

例如

data.details[i].Callsign

data.details.Callsign

答案 5 :(得分:0)

将索引添加到data.details的末尾

data.details.Dma变为data.details[i].Dma