使用jQuery循环播放对象
问题描述:
我第一次触摸JSON,并且遇到迭代对象的问题。使用jQuery循环播放对象
的JSON格式为这样:
{
"response": {
"2012-01-01": {
"Available": 99,
"Variations": [
{
"ID": 43,
"InternalItemID": "Adult",
"Price": "49.00"
}
]
},
"2012-01-02": {
"Available": 99,
"Variations": [
{
"ID": 43,
"InternalItemID": "Adult",
"Price": "49.00"
}
]
}
}
}
我能够访问日期,但不能得到任何更深。我需要所有的值:
$.getJSON(jsonurl, function(data){
$.each(data.response, function(thisDate){
alert(thisDate);
});
});
一些点我在正确的方向,请
答
$(function(){
$.each(data.response, function(index,item){
alert(item.Available);
var child=item.Variations;
alert(child[0].Price);
});
});
+0
正是我想要的。谢谢! – Fraser
答
当您需要该值时,您正在使用属性名称。
jQuery.each(collection, callback(indexInArray, valueOfElement))
所以,你可以看到日期的原因是,它是属性名称。你想:
$.each(data.response, function(thisDate, valueOfElement){
alert(thisDate, valueOfElement);
});
你不会真的有*解析* JSON字符串的问题,你呢? – Bergi