Oliver Spryn
Oliver Spryn

Reputation: 17348

Reading Dynamic JSON Array

I trying to build a JavaScript function which will grab a JSON encoded array and return a value based on the requested key. I use the jQuery $.parseJSON() method to take the JSON string and convert it into a JavaScript object. Here a watered down example:

function getValue(dynamicArrayKey) {
  var theArray = $.parseJSON(/* Get some JSON from a source using jQuery */);

  alert('Here is the value: ' + theArray.dynamicArrayKey);
}

So the key I want will be given to the function, and it should return the resulting value. I am thinking that the JavaScript eval() method should be used in there somewhere, but I'm not sure. Any help would be greatly appreciated.

Upvotes: 0

Views: 1767

Answers (2)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114367

Take a look at this. It may help.

How to search JSON tree with jQuery

Upvotes: 0

Dr.Molle
Dr.Molle

Reputation: 117324

There's no need to eval(), use

alert('Here is the value: ' + theArray[dynamicArrayKey]);

Upvotes: 4

Related Questions