这是我的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
}
]
如何计算其中的对象数量?
答案 0 :(得分:12)
这是一个阵列
您可以解析它(JSON.parse
),然后使用length
属性。
答案 1 :(得分:6)
您可以使用以下解决方案来计数JSON对象:
var jsonObject = {"test1":1,"test2":2};
var keyCount = Object.keys(jsonObject).length;
答案 2 :(得分: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.
答案 3 :(得分:2)
假设它在变量foo
,foo.length
。
var foo = [{...},{...},...];
alert(foo.length);
foo[0].id// 38;
答案 4 :(得分:2)
您还可以使用在线JSON编辑器进行验证,例如,尝试JSON Editor Online by josdejong
答案 5 :(得分:0)
此例程计算4个数组元素,每个元素有16个属性:
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);
}
答案 6 :(得分:0)
const data = JSON.parse(this.response);
var length = 0;
for (var k in data)
if (data.hasOwnProperty(k))
length++;