开发者

How to access this, is this an object literal or?

开发者 https://www.devze.com 2023-02-28 06:50 出处:网络
My javascript looks like: [{\"user\":{\"property1\":8,\"p开发者_运维技巧roperty2\":\"asdfasdf\"}}];

My javascript looks like:

[{"user":{"property1":8,"p开发者_运维技巧roperty2":"asdfasdf"}}];

I tried:

alert(user.property1);

But nothing rendered, what am I missing here?


Your javascript is an array, I assume it's assigned to a variable?

var myArray = [{"user":{"property1":8,"property2":"asdfasdf"}}];
alert(myArray[0].user.property1);


You don't seem to assign the object literal to a variable. You must do that in order to be able to reference it the way you seem to want. Note that [] indicates an array.

So you're almost there:

var myObj = [{"user":{"property1":8,"property2":"asdfasdf"}}];

alert(myObj[0].user.property1);

Your object literal creates an array, with an object that has a property named user. This user property itself is set to an object which has two properties - property1 and property2.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号