2012-03-09 22 views
6

Ecco la mia JSON:Come contare gli oggetti 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 
    } 
] 

Come faccio a contare il numero di oggetti in esso?

+0

si prega di formato del codice tempo. [JSONLint] (http://jsonlint.com/). – paislee

+0

@paislee: Grazie per la modifica! : D –

risposta

12

Questo è un array.
È possibile analizzarlo (JSON.parse), quindi utilizzare la proprietà length.

+1

Non è necessario analizzarlo se è già un array letterale, ya? – AlienWebguy

+0

@AlienWebguy: Se è nella fonte, sì. La domanda è molto vaga. – SLaks

+2

Ottengo 1253 anziché 4. – sehummel

2

Supponendo che sia nella variabile foo, foo.length.

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

Questa routine conta 4 elementi di un array con 16 attributi per elemento:

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. Solo per lunghezza! –

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.

Problemi correlati