我有类似的json数组
var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]'
如何解析数据并使用prototype.js对其进行迭代?
我有类似的json数组
var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]'
如何解析数据并使用prototype.js对其进行迭代?
有一个函数叫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
然后只需使用for
遍历数组:
for (var i=0;i<data.length;i++)
{
//do whatever you like with data[i];
}
或者使用prototypeJS.each()
函数:
data.each(function(el){
//do whatever you like with el
});