Reputation: 10611
i have come across a peculiar problem. That is i can able to access the value of a JSON array when i am doing it directly with key. But it is not getting the same when key is a variable. For eg.,
var response = {"jpg":{"table_name":"tbl_photo"}};
alert(response.jpg.table_name);
var fileType = "jpg";
alert(response.fileType.table_name);
In this, the first alert is getting triggered correctly. But in the second, where we are passing the key as a variable, it is not working.
Here is the fiddle link of this, http://jsfiddle.net/aAZeT/
Can anybody help me in this?
Upvotes: 2
Views: 893
Reputation: 275877
For strings you need index operation i.e. [] :
var response = {"jpg":{"table_name":"tbl_photo"}};
alert(response.jpg.table_name);
var fileType = "jpg";
alert(response[fileType].table_name);
Upvotes: 4