开发者

get name of json

JSON

product82127600211="a"

product82127600212="b"

product82127600213="c"

javascript

var idCompany=8212760021;
var idProduct="produc开发者_开发技巧t"+idCompany+"1";
alert(products.idProduct); // this line show undefined

this alert show undefined but if use alert(products.product82127600211); show a success.

now how can create idProduct that show a


Your syntax is wrong, use brackets instead:

alert(products[idProduct]);

Currently you are trying to access a variable named idProduct, i.e. your example would be equivalent to:

alert(products["idProduct"]);


alert(products[idProduct]); // this line show a


Would

alert(products[idProduct]);

not work?


try

alert(products[idProduct]);


To use a variable key for an JavaScript object, use the bracket notation (["key"]) instead of the dot notation (.key). In your example you would:

alert(products[idProduct])


I believe your problem is that the reference to idProduct is not attached to your object products.

e.g.

products = {};
products.idProduct="product"+idCompany+"1";
alert(products.idProduct);

The alert shows the value of idProduct

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜