1

How can I get the array values return by a Json format in twitter. I've already know how to handle json format from yahoo and fb but, the json format returned by twitter is different with a callback function.

fb

my({
   "data": [{
            "name": "May Ann Salle",
            "created_time": "2012-01-09T12:29:19+0000"
           }]
});

twitter

my([
      {
        "recipient_screen_name": "1stone"
      },
      {
        "recipient_screen_name": "2ndone"
      }
]);



$.each(my, function(i, tw) { 
        disp += tw.recipient_screen_name;   
});

alert(disp);

but I only get the 1st one, I'm unable to get the 2nd one of array.

4
  • You're missing a comma in the second example between your objects. Also, what is my? Commented Jan 16, 2012 at 18:50
  • @Joe forget to type that, but in actual output there is. my is the callback function. Commented Jan 16, 2012 at 18:53
  • Your code works fine as-is, what problem are you having? jsfiddle.net/BnCVU Commented Jan 16, 2012 at 18:57
  • @Rocket I can only get the first array, It seems that in the each function I have only one array. Commented Jan 16, 2012 at 18:59

2 Answers 2

2

You should mention this is about JSON-P not just JSON.

Anyway, you'll probably need different callbacks for each data-set.

// your callback function
function myTwitterCb(data) {
  var disp = ""
  var len = data.length
  var tw = {} // placeholder object
  for (var i = 0; i < len; i++) {
    tw = data[i]
    disp += tw.recipient_screen_name
  }
  doSomethingElseWith(disp)
}

function myFacebookCb(response) {
  var data = response.data
  var len = data.length
  var fb = {} // placeholder object
  for (var i = 0; i < len; i++) {
    fb = data[i]
    disp += fb.name
  }
  doSomethingElseWith(disp)


}

function doSomethingElseWith(disp) {
  // draw it to the screen or something
}
2

This should work:

var disp, i = 0, l = tw.length;
for(i; i < l; i++) {
    disp += tw[i].recipient_screen_name;
}

alert(disp);

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.