I have json array like
var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]'
How to parse the the data and iterate through it using prototype.js?
I have json array like
var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]'
How to parse the the data and iterate through it using prototype.js?
There is a function called evalJSON()
var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]';
data = data.evalJSON();
//for more security:
data = data.evalJSON(true); //sanitizes data before processing it
Then just use for
to iterate through the array:
for (var i=0;i<data.length;i++)
{
//do whatever you like with data[i];
}
Or use the prototypeJS .each()
function:
data.each(function(el){
//do whatever you like with el
});