Если test
является строкой, содержащей JSON, вы можете проанализировать ее с помощью jQuery.parseJSON
, которая вернет объект JavaScript.
Если test
написано так:
var test = [
{
"id": "1",
"name": "Boat"
},
{
"id": "2",
"name": "Cable"
}
];
... это уже является объектом JavaScript;конкретно массив.jQuery.each
будет проходить по каждой записи массива.Если вы также хотите просмотреть свойства этих записей, вы можете использовать второй цикл:
$.each(test, function(outerKey, outerValue) {
// At this level, outerKey is the key (index, mostly) in the
// outer array, so 0 or 1 in your case. outerValue is the
// object assigned to that array entry.
$.each(outerValue, function(innerKey, innerValue) {
// At this level, innerKey is the property name in the object,
// and innerValue is the property's value
});
});
Живой пример