繁体   English   中英

遍历并显示json数据

[英]Iterating through and displaying json data

我有一个codeigniter应用程序,在我的一种观点中,我有一个对返回json数据的API的ajax调用。 一旦获得数据,便会遍历数据并将记录追加到现有表中。 有两种方法可以运行此ajax调用。 一种是请求“所有”数据,另一种是按位置过滤。 两种方法都返回数据,但是当Im试图遍历过滤后的数据集时,ajax失败了。

这是遍历记录集的代码:

if (JSONdata.length != 0) 
{
        //create a heading row and attach it to the existing table.
        var heading = $('<tr id="tblheading" naming="tblheading">').appendTo($('#switchrecords'));
        heading.append($('<td>').append().text('Name'));
        heading.append($('<td>').append().text('Object ID'));

        //loop through each JSONdata item in the array and append another row to the switchrecords table.
        $.each(JSONdata, function(i, objswitch) {                           
            console.log('objectid is:'.objswitch.id);
            console.log(objswitch.id);
            var row = $('<tr>').appendTo($('#switchrecords'));
            row.append($('<td>').append($('<a href='+ BASEPATH + 'index.php/controller/methodname/' + objswitch.id + '>').text(objswitch.name)));
            row.append($('<td>').append(objswitch.id).text(objswitch.id));
});

到目前为止,这是我所做的:

  1. 我确保两个结果集都具有相同的字段,即“ id”和“ name”。 请注意,经过过滤的数据集比未经过滤的结果包含更多的字段,但我认为这并不重要。

  2. 我已经使用console.log来转储这两个结果集...这是这两个的摘要。 第一个是ALL,另一个被过滤。

    [{“名称”:“ 888-12-993-99-1”,“ id”:“ 1”,“ dict_value”:“紧凑”},{“名称”:“ 888-22-SR1-RTR-1 “,” id“:” 2“,” dict_value“:” compact“},{” name“:” 888-21-SR1-SW-1“,” id“:” 3“,” dict_value“:” compact “},{” name“:” 888-11-SR2-SW-2“,” id“:” 4“,” dict_value“:” compact“},.. etc

    [{{“ parent_id”:“ 2”,“ tag_id”:“ 10”,“ Location”:“ Building1”,“ id”:“ 7”,“ name”:“ 888-22-228-22-1”, “ label”:null,“ asset_no”:“ 1026067”,“ objtype_id”:“ 1503”},{“ parent_id”:“ 2”,“ tag_id”:“ 5”,“ Location”:“ Building2”,“ id “:” 6“,”名称“:” 888-2-263-88-1“,”标签“:空,” asset_no“:” 1026068“,” objtype_id“:” 1503“},....等。

  3. 从代码片段中可以看到,我尝试添加一些调试信息以查看循环中发生了什么。 但是,我在第一个console.log()调用中收到以下错误消息:

    [17:13:30.675] TypeError:“对象ID为:”。objswitch未定义

我不太确定该如何解决。 任何建议,将不胜感激。 如果这是一个愚蠢的错误,我提前致歉! 我整天都在编程,我的大脑糊涂了! =)

先谢谢您的帮助。

除非我完全不喜欢,否则看起来.objswitch应该是+ objswitch

尝试

console.log('objectid is:' + objswitch.id);

代替

console.log('objectid is:'.objswitch.id);

看起来您的语法连接错误。.应该使用+完成,我看到您使用的是 代替objswitch尝试使用此

$.each(JSONdata, function(i) {                           
    console.log('objectid is:' +  this.id);
    console.log(this.id);
    var row = $('<tr>').appendTo($('#switchrecords'));
    row.append($('<td>').append($('<a href='+ BASEPATH + 'index.php/switches/getswitchdetails/' + this.id + '>').text(this.name)));
    row.append($('<td>').append(this.id).text(this.id));
});

否则,一般尝试使用变量

$.each(JSONdata, function(i) {                           
    console.log('objectid is:'+  JSONdata[i].id);
    console.log(JSONdata[i].id);
    var row = $('<tr>').appendTo($('#switchrecords'));
    row.append($('<td>').append($('<a href='+ BASEPATH + 'index.php/switches/getswitchdetails/' + JSONdata[i].id + '>').text(JSONdata[i].name)));
    row.append($('<td>').append(JSONdata[i].id).text(JSONdata[i].id));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM