访问json中的数据未获取密钥 [英] access data in json not getting the key

查看:128
本文介绍了访问json中的数据未获取密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var data = [{
	"error": false,
	"date": "09-02-2016",
	"day": "5",
	"checkin": "20:29:11",
	"checkout": null,
	"break_timein": null,
	"break_timeout": null,
	"checkin_remarks": "test",
	"checkout_remarks": null,
	"break_time_remarks": null
}];



console.log(data[0].checkin)
console.log(data[0].break_timein)
console.log(data[0].break_timein.length > 0 || data[0].break_timein != null)

对于break_timein,当我console.log时,它返回null,但是当我得到长度时,它返回Cannot read property 'length' of null

For break_timein when i console.log it, it returns null but when I get the length it returns Cannot read property 'length' of null

这是为什么?

推荐答案

我知道它只会在没有属性break_timein时出现,而只有它在null

I understand that it only appears when there is not property break_timein but it is there its just null

您在这里得出了错误的结论.该错误与属性是否存在无关.

You drew the wrong conclusion here. The error doesn't have anything to do with whether a property exists or not.

当某个属性不存在并且您尝试访问该属性时,会出现以下错误:

When a property doesn't exist and you are trying to access a property on it you are getting the following error:

var foo = {};
foo.bar.baz;
// Uncaught TypeError: Cannot read property 'baz' of undefined

此句子中的未定义"不是指属性的 existence ,而是指 undefined.在JavaScript中,当访问属性不存在时,对其进行访问将返回值undefined:

The "undefined" in this sentence doesn't refer to the existence of the property, it refers to the value undefined. In JavaScript, when accessing a property doesn't exist, accessing it will return the value undefined:

foo.bar
// undefined

在其他情况下也会创建undefined,例如当您定义没有初始值的变量时:

There are other situations where undefined is created, e.g. when you define a variable without an initial value:

var abc;
abc;
// undefined

知道我们可以测试直接访问undefined上的属性时会发生什么:

Knowing that we can test what happens when we directly access a property on undefined:

undefined.foo
//  Uncaught TypeError: Cannot read property 'foo' of undefined

这将引发与上述相同的错误,因此它与属性访问没有任何关系.

This throws the same error as above, so it doesn't have anything to do with property access.

因此,现在我们已经确定undefined不能在属性访问上下文中使用.

So now we have established that undefined cannot be used in a property access context.

null是另一个与此类似的值:

null is another value that is just like that:

null.foo
// Uncaught TypeError: Cannot read property 'foo' of null

nullundefined与其他显示此行为的原始值(字符串,数字,布尔值)有何不同?

What makes null and undefined different from other primitive values (string, number, boolean) that they show this behavior?

nullundefined实际上是两个唯一数据类型的值:Null和Undefined.

null and undefined are actually values of two unique data types: Null and Undefined.

那些数据类型没有没有对象包装器.字符串,数字和布尔值可以两种形式存在:作为原始值和作为对象值:

Those data types do not have an object wrapper. Strings, Numbers and Boolean values can exist in two forms: as primitive values and as object value:

var primitive = "foo";
var object = new String("foo");

当您访问原始值上的属性时,例如"foo".length,JavaScript 自动转换原始值 temporary 对象值,类似于new String("foo").length.

When you are accessing a property on a primitive value, e.g. "foo".length, JavaScript automatically converts the primitive value to a temporary object value, similar to new String("foo").length.

但是,由于没有等效于Null and Undefined的对象(通过没有NullUndefined函数这一事实就可以看出),因此无法完成操作,因此会引发错误.

But since there is no object equivalent for Nulland Undefined (evident by the fact that there is no Null or Undefined function), this cannot be done, so an error is thrown.

这篇关于访问json中的数据未获取密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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