我有像
这样的json数组var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]'
如何解析数据并使用prototype.js迭代它?
答案 0 :(得分:1)
有一个名为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
});