Javascript 从我的json响应中访问一组特定的值?
我正在尝试使用for..in循环访问“Event”中的描述键/值,但目前不完全确定如何实现这一点。首先,我使用for..in并将其注销,这将返回响应中的所有顶级条目,我现在如何深入并选择Event.description?我首先认为是数据[prop].Event.description,但事实并非如此。我应该使用一个普通的for循环,然后在这个循环的内部使用for 这是我目前的代码:Javascript 从我的json响应中访问一组特定的值?,javascript,jquery,json,Javascript,Jquery,Json,我正在尝试使用for..in循环访问“Event”中的描述键/值,但目前不完全确定如何实现这一点。首先,我使用for..in并将其注销,这将返回响应中的所有顶级条目,我现在如何深入并选择Event.description?我首先认为是数据[prop].Event.description,但事实并非如此。我应该使用一个普通的for循环,然后在这个循环的内部使用for 这是我目前的代码: $(document).ready(function() { var data = {
$(document).ready(function() {
var data = {
"status": "ok",
"code": "200",
"message": "event details",
"data": [{
"Event": {
"id": "1",
"name": "Sample Event Number 1",
"description": "Sample Event Number 4 Description ....",
"event_date": "2012-05-31 00:00:00",
"Band": [{
"id": "1",
"name": "Support #1",
"BandsEvent": {
"id": "7",
"band_id": "2",
"event_id": "8",
"created": "2012-05-23 15:53:56",
"modified": "2012-05-23 15:53:56"
}},
{
"id": "2",
"name": "Support #2",
"BandsEvent": {
"id": "8",
"band_id": "1",
"event_id": "8",
"created": "2012-05-23 15:53:57",
"modified": "2012-05-23 15:53:57"
}}]
}},
{
"Event": {
"id": "2",
"name": "Sample Event Number 2",
"description": "Sample Event Number 4 Description ....",
"event_date": "2012-05-31 00:00:00",
"Band": [{
"id": "2",
"name": "Another Crazy Band",
"BandsEvent": {
"id": "3",
"band_id": "2",
"event_id": "8",
"created": "2012-05-23 15:53:56",
"modified": "2012-05-23 15:53:56"
}},
{
"id": "4",
"name": "The Band",
"BandsEvent": {
"id": "8",
"band_id": "1",
"event_id": "8",
"created": "2012-05-23 15:53:57",
"modified": "2012-05-23 15:53:57"
}}]
}}]
}
var prop;
for (prop in data) {
console.log( data[prop] );
// data.Event.description
}
});
这应该满足您的要求:
for (var i = 0; i < data.data.length; i++) {
console.log(data.data[i].Event.description);
}
我建议存储数组并在上面循环。:
for(var events=data.data,I=0,n=events.length;I
for (var i = 0; i < data.data.length; i++) {
console.log(data.data[i].Event.description + " has the following bands:");
for (var j = 0; j < data.data[i].Event.Band.length; j++) {
console.log(data.data[i].Event.Band[j].name);
}
}