Page 1 of 1

JSON key from variable

Posted: Thu Mar 04, 2010 11:02 am
by Ragnis

Code: Select all

 
var key = "b";
var json = {
    "a": "first",
    "b": "second",
    "c": "third"
};
 
Now i need to do something like this:

Code: Select all

 
alert(json.key);
 
And it should display "second".

But i don't know how to do it.

Re: JSON key from variable

Posted: Thu Mar 04, 2010 11:03 am
by Weirdan

Code: Select all

 alert(json[key]);