javascript - accessing a specific set of values from within my json response? -


i trying use for..in loop access description key/value within 'event' @ moment not sure how achieve this. first of used for..in , logged out, returns top level entries in response, how drill down , pick out event.description? first of thought data[prop].event.description thats not case. should using normal loop , for..in inside of this?

here current code:

$(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;      (prop in data) {         console.log( data[prop] );         // data.event.description      }  });​ 

this should want:

for (var = 0; < data.data.length; i++) {     console.log(data.data[i].event.description);         } 

i should add reason code doesn't work "prop" variable first "status", "code", "message" , "data". status/code/message has no "event" property, therefore code return undefined if you'd try access data[prop].event. here pick them out specifically. , since data.data array, there's no reason use for .. in loop, rather regular loop.

likewise, if want print out descriptions , bands, following:

for (var = 0; < data.data.length; i++) {     console.log(data.data[i].event.description + " has following bands:");      (var j = 0; j < data.data[i].event.band.length; j++) {           console.log(data.data[i].event.band[j].name);      }        } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -