从Json格式Javascript获取数组值

时间:2022-09-10 13:38:14

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.

如何通过twitter中的Json格式返回数组值。我已经知道如何处理来自yahoo和fb的json格式但是,twitter返回的json格式与回调函数不同。

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.

但我只得到第一个,我无法获得第二个阵列。

2 个解决方案

#1


2  

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
}

#2


2  

This should work:

这应该工作:

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

alert(disp);

#1


2  

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
}

#2


2  

This should work:

这应该工作:

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

alert(disp);