views:

22

answers:

1

I'm having a bit of trouble working something out with regards to javascript and Json.

I have a function that contains a json object

blah=function(i){
var hash= ({
  "foo" : "bar",
  "eggs":"bacon",
  "sausage":"maple syrup"
  });
var j=eval(hash); // Convert to Object
console.log(j.toSource()); // Yes I know it's only in firefox!
console.log(j.i); // Attempt to get the value of for example foo - which is bar
}

then call the function with blah('foo'); to attempt it to console log "bar" form the json object.

THe trouble is all I get is "undefined" because the function is treating "i" as a string.

My quertion is how can I typecast the "i" variable to be soemthing that can access the json object.

Please help .. my head hurts and google has coem up short!.

Thanks in advance

Alex

+2  A: 

Well...

j[i]

:)

Bart van Heukelom
Unbelieveable .. I am such an idiot lol .. I had a feeling it would be something simple!
Alex