在$ .each的JSON文件末尾显示未定义

时间:2017-09-06 11:50:38

标签: javascript jquery json ajax

我正在尝试过滤项目所需的数据(我只想要日期)。我设法循环遍历JSON对象并显示它。唯一的问题是,在循环结束时,它显示未定义。我不知道为什么我做错了。这是我做的一个例子

JS档案

console.clear();

$.ajax({
  async: true,
  dataType: "json",
  url: "https://api.myjson.com/bins/n1cwx",
  success: function(data) {
    $.each(data, function (i, object) {
      //console.log(data);
      //console.log(object);
      $("body").append("<p>" + object.last_activity_at + "</p>");           
    });
  }
});

输出

2017-06-09T17:49:49.975+12:00

2017-07-13T08:59:20.023+12:00

2017-05-29T14:17:18.148+12:00

2017-05-21T22:19:07.951+12:00

2017-06-09T13:59:50.464+12:00

2017-06-13T08:18:30.340+12:00

2017-07-27T12:41:17.423+12:00

undefined

https://codepen.io/anon/pen/gxyZXm

谢谢!

5 个答案:

答案 0 :(得分:2)

JSON数据中的最后一个元素不是对象,它是:

pages: "1"

因此,object.last_activity_at未定义。

解决方法是:

$.each(data, function (i, object) {
      if(!object.last_activity_at) return;
      $("body").append("<p>" + object.last_activity_at + "</p>");           
});

答案 1 :(得分:1)

您的API正在向响应中添加最后一个属性(页面:1)。这会将您的对象数组转换为键控对象。因此,现在你的循环正在解析&#34; pages&#34;作为的最终对象具有您尝试注入HTML的属性,因此未定义

修复数组,或忽略循环中的最后一个对象值。

{ "pages":"1" } // No last_activity_at property here.

答案 2 :(得分:1)

你有一个包含7个对象的对象,最后是一个键/值对,因此你的错误是:

{  
   "0":{  },
   "1":{  },
   "2":{  },
   "3":{  },
   "4":{  },
   "5":{  },
   "6":{  },
   "pages":"1"
}

在尝试访问之前,您应该检查以确保每个对象实际包含last_activity_at

if ( typeof object !== 'undefined' && object.hasOwnProperty('last_activity_at') ) {
    $("body").append("<p>" + object.last_activity_at + "</p>");
}

答案 3 :(得分:1)

你的数组包含这样一个对象:

enter image description here

正如您所见,最后一项是

pages : 1 // causing problem

因此,最后一项未定义。因此要么删除pages : 1,要么删除:

console.clear();

$.ajax({
  async: true,
  dataType: "json",
  url: "https://api.myjson.com/bins/n1cwx",
  success: function(data) {
    $.each(data, function (i, object) {
      if(object.last_activity_at)
      $("body").append("<p>" + object.last_activity_at + "</p>");           
    });
  }
});

答案 4 :(得分:0)

您的JSON不是数组:

{
  "0": {
    "id": 34,
    "description": "A collection of in houses",
    "default_branch": "master",
    "tag_list": [],
    "created_at": "2017-02-28T15:02:49.756+13:00",
    "last_activity_at": "2017-06-09T17:49:49.975+12:00",
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 13,
    "namespace": {
      "id": 99,
      "parent_id": 98
    },
    "star_count": 0,
    "forks_count": 0,
    "open_issues_count": 0,
    "public_builds": true,
    "shared_with_groups": [],
    "only_allow_merge_if_build_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 40,
        "notification_level": 3
      }
    }
  },
  "1": {
    "id": 38,
    "description": "Scripts",
    "default_branch": "master",
    "tag_list": [],
    "created_at": "2017-02-28T15:05:40.505+13:00",
    "last_activity_at": "2017-07-13T08:59:20.023+12:00",
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 13,
    "namespace": {
      "id": 99,
      "parent_id": 98
    },
    "star_count": 0,
    "forks_count": 0,
    "open_issues_count": 0,
    "public_builds": true,
    "shared_with_groups": [],
    "only_allow_merge_if_build_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 40,
        "notification_level": 3
      }
    }
  },
  "2": {
    "id": 49,
    "description": "Dashboard",
    "default_branch": "master",
    "tag_list": [],
    "created_at": "2017-04-26T14:50:59.051+12:00",
    "last_activity_at": "2017-05-29T14:17:18.148+12:00",
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 14,
    "namespace": {
      "id": 99,
      "parent_id": 98
    },
    "star_count": 0,
    "forks_count": 0,
    "open_issues_count": 0,
    "public_builds": true,
    "shared_with_groups": [],
    "only_allow_merge_if_build_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 40,
        "notification_level": 3
      }
    }
  },
  "3": {
    "id": 51,
    "description": "",
    "default_branch": "master",
    "tag_list": [],
    "created_at": "2017-05-18T16:33:48.041+12:00",
    "last_activity_at": "2017-05-21T22:19:07.951+12:00",
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 19,
    "namespace": {
      "id": 24,
      "parent_id": null
    },
    "avatar_url": null,
    "star_count": 0,
    "forks_count": 0,
    "open_issues_count": 0,
    "public_builds": true,
    "shared_with_groups": [],
    "only_allow_merge_if_build_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "permissions": {
      "project_access": {
        "access_level": 40,
        "notification_level": 3
      },
      "group_access": null
    }
  },
  "4": {
    "id": 60,
    "description": "",
    "default_branch": "master",
    "tag_list": [],
    "created_at": "2017-06-09T13:59:50.464+12:00",
    "last_activity_at": "2017-06-09T13:59:50.464+12:00",
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 14,
    "namespace": {
      "id": 99,
      "parent_id": 98
    },
    "star_count": 0,
    "forks_count": 0,
    "open_issues_count": 0,
    "public_builds": true,
    "shared_with_groups": [],
    "only_allow_merge_if_build_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 40,
        "notification_level": 3
      }
    }
  },
  "5": {
    "id": 62,
    "description": "Mattermost Android App",
    "default_branch": "master",
    "tag_list": [],
    "created_at": "2017-06-13T08:18:30.340+12:00",
    "last_activity_at": "2017-06-13T08:18:30.340+12:00",
    "shared_runners_enabled": true,
    "lfs_enabled": true,
    "creator_id": 13,
    "namespace": {
      "id": 99,
      "parent_id": 98
    },
    "star_count": 0,
    "forks_count": 0,
    "open_issues_count": 0,
    "public_builds": true,
    "shared_with_groups": [],
    "only_allow_merge_if_build_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 40,
        "notification_level": 3
      }
    }
  },
  "6": {
    "id": 283,
    "description": "",
    "default_branch": null,
    "tag_list": [],
    "created_at": "2017-07-24T16:08:01.893+12:00",
    "last_activity_at": "2017-07-27T12:41:17.423+12:00",
    "shared_runners_enabled": true,
    "lfs_enabled": false,
    "creator_id": 23,
    "namespace": {
      "id": 99,
      "parent_id": 98
    },
    "star_count": 0,
    "forks_count": 0,
    "open_issues_count": 1,
    "public_builds": true,
    "shared_with_groups": [],
    "only_allow_merge_if_build_succeeds": false,
    "request_access_enabled": false,
    "only_allow_merge_if_all_discussions_are_resolved": false,
    "permissions": {
      "project_access": null,
      "group_access": {
        "access_level": 40,
        "notification_level": 3
      }
    }
  },
  "pages": "1"
}

这个对象碰巧有一些恰好是数字字符串的属性,还有一个名为“pages”的属性。 $.each()循环遍历地图的所有键,包括“页面”。

解决问题的方法是:

  • 您可以生成一个不那么令人困惑的JSON,这将使您免于未来的麻烦;
  • 您可以事先过滤键,只选择数字键;
  • 或许,您可以在each()内检查密钥是否为字符串;
  • 正如其他人已经提到的那样,另一个选择是跳过没有你需要密钥的对象。