如何计算JSON对象

23

这是我的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
    }
]

我该如何计算其中的对象数量?


1
JSONLint - calebds
请将标题更改为传达您想要计算数组元素而不是对象的意思。以下是程序相关内容的翻译: - Nikhil VJ
7个回答

46

您可以使用以下解决方案来计算JSON对象:

var jsonObject = {"test1":1,"test2":2};
var keyCount  = Object.keys(jsonObject).length;

谢谢!我正在寻找这个,而不是计算数组元素。 - Nikhil VJ

29

这是一个数组。
你可以解析它(JSON.parse),然后使用 length 属性。


@AlienWebguy:如果这是源代码中的话,是的。问题非常模糊。 - SLaks
明白了。谢谢。对于格式不佳的代码感到抱歉。而且,我表述得有点含糊。 - sehummel
如果我需要用PHP怎么办? - Ali Raza

5

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.


4
假设变量名为foo,则foo.length表示其长度。
var foo = [{...},{...},...];
alert(foo.length);
foo[0].id// 38;

3

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);
}

0
const data = JSON.parse(this.response);

var length = 0;

for (var k in data) 
  if (data.hasOwnProperty(k)) 
     length++;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接