2017-09-06 57 views
0

我試圖篩選,我需要爲我的項目的數據。每$結束不確定的(我只想只有日期)。我設法遍歷JSON對象並顯示它。唯一的問題是,在循環結束時,它顯示未定義。我不知道我爲什麼做錯了。這裏是我做過什麼顯示在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

感謝一個例子!

+0

它可以是API的有板有眼「未定義」回來了?似乎你的代碼沒有問題,但當日期提交到數據庫時可能出現錯誤? – Maartje

回答

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>");   
}); 
+0

thansk這麼多!這工作正常。我真的沒有太多的控制與JSON文件的內容,所以真的需要一個工作。幾分鐘後會接受你的答案。再次感謝! –

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" 
} 

這是發生在有這種情況發生是數字串幾個屬性的對象,再加上一個屬性名爲'網頁'。 $.each()通過地圖的所有鍵,包括「頁」循環。

途徑SOVE問題是:

  • 您可以生成一個不太混亂JSON,這將節省你從未來的頭痛;
  • 你可以事先篩選鍵,選擇只有數字的人;
  • ,你可以,也許,裏面each()檢查如果該鍵是一個字符串;
  • 另一種選擇,前面已經提到別人,會跳過沒有你所需要的關鍵的對象。
+0

但這並不能解釋或解決問題。 –

1

您的API正在將最後一個屬性添加到響應中(頁面:1)。這將您的對象數組轉換爲一個帶鍵的對象。正因爲如此,現在你的循環解析「頁面」作爲它的最終對象不是有你正試圖注入HTML的屬性,因此undefined

解決您的陣列,或忽略在循環的最後一個對象的值。

{ "pages":"1" } // No last_activity_at property here. 
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>"); 
} 
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>");   
    }); 
    } 
});