4

我有类似的json数组

var data = '[{ "name": "Violet", "occupation": "character" },{ "name": "orange", "occupation": "color" }]'

如何解析数据并使用prototype.js对其进行迭代?

4

1 回答 1

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
});
于 2013-03-26T12:40:16.370 回答