2012-03-09 40 views
6

Đây là JSON của tôi:Làm thế nào để đếm JSON đối tượng

[ 
    { 
     "id": "38", 
     "article_id": "16", 
     "news_event": "625", 
     "language": "en", 
     "channel_partner_id": "625", 
     "title": "Test", 
     "show_logo": null, 
     "description": "test\n\n", 
     "schedule": null, 
     "event_date": "2012-03-09 10:08:35", 
     "link_text": null, 
     "guid": null, 
     "timestamp": "2012-03-09 11:19:42", 
     "website": null, 
     "show_hours": null, 
     "page_text": null 
    }, 
    { 
     "id": "37", 
     "article_id": "15", 
     "news_event": "625", 
     "language": "en", 
     "channel_partner_id": "625", 
     "title": "Test", 
     "show_logo": null, 
     "description": "test\n\n", 
     "schedule": null, 
     "event_date": "2012-03-09 10:08:35", 
     "link_text": null, 
     "guid": null, 
     "timestamp": "2012-03-09 11:19:39", 
     "website": null, 
     "show_hours": null, 
     "page_text": null 
    }, 
    { 
     "id": "36", 
     "article_id": "14", 
     "news_event": "625", 
     "language": "en", 
     "channel_partner_id": "625", 
     "title": "Test", 
     "show_logo": null, 
     "description": "test\n\n", 
     "schedule": null, 
     "event_date": "2012-03-09 10:08:35", 
     "link_text": null, 
     "guid": null, 
     "timestamp": "2012-03-09 11:19:35", 
     "website": null, 
     "show_hours": null, 
     "page_text": null 
    }, 
    { 
     "id": "35", 
     "article_id": "13", 
     "news_event": "625", 
     "language": "en", 
     "channel_partner_id": "625", 
     "title": "Test", 
     "show_logo": null, 
     "description": "test\n\n", 
     "schedule": null, 
     "event_date": "2012-03-09 10:08:35", 
     "link_text": null, 
     "guid": null, 
     "timestamp": "2012-03-09 11:19:31", 
     "website": null, 
     "show_hours": null, 
     "page_text": null 
    } 
] 

Làm thế nào để đếm số lượng các đối tượng trong nó?

+0

xin định dạng mã dài. [JSONLint] (http://jsonlint.com/). – paislee

+0

@paislee: Cảm ơn bạn đã chỉnh sửa! : D –

Trả lời

12

Đó là một mảng.
Bạn có thể phân tích cú pháp (JSON.parse), sau đó sử dụng thuộc tính length.

+1

Không cần phải phân tích cú pháp nếu nó đã là một mảng chữ, ya? – AlienWebguy

+0

@AlienWebguy: Nếu đó là nguồn, vâng. Câu hỏi là rất mơ hồ. – SLaks

+2

Tôi nhận được 1253 thay vì 4. – sehummel

2

Giả sử nó nằm trong biến foo, foo.length.

var foo = [{...},{...},...]; 
alert(foo.length); 
foo[0].id// 38; 
0

này thói quen đếm 4 phần tử mảng với 16 thuộc tính cho mỗi nguyên tố:

var obj = [{ "id": "38", "article_id": "16", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:42", "website": null, "show_hours": null, "page_text": null }, { "id": "37", "article_id": "15", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:39", "website": null, "show_hours": null, "page_text": null }, { "id": "36", "article_id": "14", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:35", "website": null, "show_hours": null, "page_text": null }, { "id": "35", "article_id": "13", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:31", "website": null, "show_hours": null, "page_text": null}] 
for (var i = 0; i < obj.length; i++) { 
    var ctr=0; 
    for (attr in obj[i]) ctr++; 
    alert('array['+i+']: ' +ctr); 
} 
+0

No. Chỉ cần obj.length! –

3

var json = [{ "id": "38", "article_id": "16", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:42", "website": null, "show_hours": null, "page_text": null }, { "id": "37", "article_id": "15", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:39", "website": null, "show_hours": null, "page_text": null }, { "id": "36", "article_id": "14", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:35", "website": null, "show_hours": null, "page_text": null }, { "id": "35", "article_id": "13", "news_event": "625", "language": "en", "channel_partner_id": "625", "title": "Test", "show_logo": null, "description": "test\n\n", "schedule": null, "event_date": "2012-03-09 10:08:35", "link_text": null, "guid": null, "timestamp": "2012-03-09 11:19:31", "website": null, "show_hours": null, "page_text": null}] 
 

 

 
//Object.keys(json).length --> USE 
 

 
for (var i = 1, l = Object.keys(json).length; i <= l; i++) { 
 

 
} 
 

 
//by:Jorge Nones, Jales.

Các vấn đề liên quan