1

When making a Javascript API call, I get my results back like this:

{
"data": {
    "2 Broke Girls": {
        "air_by_date": 0, 
        "cache": {
            "banner": 1, 
            "poster": 1
        }, 
        "language": "en", 
        "network": "CBS", 
        "next_ep_airdate": "2014-02-24", 
        "paused": 0, 
        "quality": "HD", 
        "show_name": "2 Broke Girls", 
        "status": "Continuing", 
        "tvdbid": 248741, 
        "tvrage_id": 28416, 
        "tvrage_name": "2 Broke Girls"
    }, 
    "Alias": {
        ...
    },
    "message": "", 
    "result": "success"
    }
}

How can i successfully iterate through all the different shows that are being returned ? I can't use response.data.SHOWNAME because the shows will be changing around alot.

I tried a couple of solutions using $.each() but i didn't really get those to work. I can get as far as accessing each show title on it's own, but the other data keeps getting returned as being [Object object]

Edit: To make it a little bit clearer, i need access to the field. response.data.RANDOM_TITLE.tvrage_name & response.data.RANDOM_TITLE.status

2
  • +1 for having Alias on that list. Commented Feb 17, 2014 at 15:01
  • How do you know that "message" is not a show? Because it's not an object? Commented Feb 17, 2014 at 15:02

5 Answers 5

2

Me too!

function getShows (json) {

    // grab just the data part
    var data = JSON.parse(json).data,
        i,

        // empty array for your shows
        shows = [];

    // delete the crap you don't want
    delete data.message;
    delete data.result;

    // loop through everything else
    for (i in data) {

        // make sure it's an own property
        if (data.hasOwnProperty(i)) {

            // add to you collection of shows
            shows.push(data[i]);
        }
    }

    // tada
    return shows;
}
Sign up to request clarification or add additional context in comments.

2 Comments

got it to work based on this, i owe you a beer or a cookie... your choice.
@Crimson document.cookie = "flavor=beer";
0

You can access the data like :

for(var show_name in response.data) {
    var show = response.data[show_name];
    if(!(show instanceof Object)) continue;
    console.log(show_name, show); 
    //note that not all browsers will print the "show" object, 
    //IE will just say [object]
}

Comments

0

You can assign the returned data into a variable.

eg

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

1 Comment

even better. just take the jsonData[0] with your json items
0

You can access like this:

var dkeys = Object.keys(d.data);
for (var i=0; i < dkeys.length; i++) {
    if (typeof d.data[dkeys[i]].show_name !== 'undefined') {
        alert(d.data[dkeys[i]].show_name);
    }
}

Comments

0
for (name in data) {
   if (data.hasOwnProperty(name)){
      //code here
      //make sure to ignore message and result and any other special results returned. 
      showDetails = data[name];
   }
}

Where data here is the returned response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.