如何查看大JSON对象是否包含值? [英] how do i see if a big JSON object contains a value?

查看:175
本文介绍了如何查看大JSON对象是否包含值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP对大型事件的多维数组进行json编码,所以我得到了以下信息:

I'm using PHP to json encode a massive multi-dimensional array of events, so i get something like this:

var ents = {"7":{"event_id":"7","nn":"The Whisky Drifters","nn_url":"the-whisky-drifters",
  "venue":"The Grain Barge","date_num":"2010-06-11","date_txt":"Friday 11th June",
  "gig_club":"1","sd":"A New Acoustic String Band...","ven_id":"44",
  "art":0},"15":{"event_id":"15","nn":"Bass Kitchen","nn_url":"bass-kitchen",
  "venue":"Timbuk2","date_num":"2010-06-11","date_txt":"Friday 11th June",
  "gig_club":"2","sd":"Hexadecimal \/ DJ Derek \/ Id","ven_id":"21",
  "art":1},

第一个维度是ID,请参见

the first dimension is the id, see

var ents = {"7":{

因此可以在不检查嵌套对象的情况下获取ID ...

So it's possible to get the ids without examining the nested objects...

检查我的JSON是否包含ID的最快,最有效的方法是什么?

推荐答案

您可以使用 hasOwnProperty 方法:

You can use the hasOwnProperty method:

if (ents.hasOwnProperty('7')) {
  //..
}

此方法检查对象是否包含指定的属性,而不管其值如何.

This method checks if the object contains the specified property regardless of its value.

in 运算符要快,因为不会检查继承的属性.

Is faster than the in operator because it doesn't checks for inherited properties.

这篇关于如何查看大JSON对象是否包含值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆