简体   繁体   English

从Json格式的Javascript获取数组值

[英]Get array values from Json format Javascript

How can I get the array values return by a Json format in twitter. 如何获取Twitter中Json格式返回的数组值。 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. 我已经知道如何处理来自Yahoo和fb的json格式,但是,twitter返回的json格式与回调函数不同。

fb 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. 但是我只能得到第一个,而我不能得到数组的第二个。

This should work: 这应该工作:

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

alert(disp);

You should mention this is about JSON-P not just JSON. 您应该提到这是关于JSON-P的,而不仅仅是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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM