-1

我想从 twitter api 检索到的 json 数组中检索趋势值。我需要前 5 个趋势名称。

json响应

[{
    "trends": [
        {
            "name": "#NationalTellAGirlSheIsBeautifulDay",
            "url": "http://twitter.com/search?q=%23NationalTellAGirlSheIsBeautifulDay",
            "promoted_content": null,
            "query": "%23NationalTellAGirlSheIsBeautifulDay",
            "events": null
        },
        {
            "name": "#YouKnowYouInTheHoodWhen",
            "url": "http://twitter.com/search?q=%23YouKnowYouInTheHoodWhen",
            "promoted_content": null,
            "query": "%23YouKnowYouInTheHoodWhen",
            "events": null
        },
        {
            "name": "#DünyaFenerbahçelilerGünüKutluOlsun",
            "url": "http://twitter.com/search?q=%23D%C3%BCnyaFenerbah%C3%A7elilerG%C3%BCn%C3%BCKutluOlsun",
            "promoted_content": null,
            "query": "%23D%C3%BCnyaFenerbah%C3%A7elilerG%C3%BCn%C3%BCKutluOlsun",
            "events": null
        },
        {
            "name": "#IndonesianTeenager",
            "url": "http://twitter.com/search?q=%23IndonesianTeenager",
            "promoted_content": null,
            "query": "%23IndonesianTeenager",
            "events": null
        },
        {
            "name": "#bpk13",
            "url": "http://twitter.com/search?q=%23bpk13",
            "promoted_content": null,
            "query": "%23bpk13",
            "events": null
        },
        {
            "name": "HaykırırımAşkımıSana BEŞİKTAŞdiye",
            "url": "http://twitter.com/search?q=%22Hayk%C4%B1r%C4%B1r%C4%B1mA%C5%9Fk%C4%B1m%C4%B1Sana+BE%C5%9E%C4%B0KTA%C5%9Ediye%22",
            "promoted_content": null,
            "query": "%22Hayk%C4%B1r%C4%B1r%C4%B1mA%C5%9Fk%C4%B1m%C4%B1Sana+BE%C5%9E%C4%B0KTA%C5%9Ediye%22",
            "events": null
        },
        {
            "name": "Vogue Korea",
            "url": "http://twitter.com/search?q=%22Vogue+Korea%22",
            "promoted_content": null,
            "query": "%22Vogue+Korea%22",
            "events": null
        },
        {
            "name": "PNG",
            "url": "http://twitter.com/search?q=PNG",
            "promoted_content": null,
            "query": "PNG",
            "events": null
        },
        {
            "name": "Makueni",
            "url": "http://twitter.com/search?q=Makueni",
            "promoted_content": null,
            "query": "Makueni",
            "events": null
        },
        {
            "name": "Jeremy Kyle",
            "url": "http://twitter.com/search?q=%22Jeremy+Kyle%22",
            "promoted_content": null,
            "query": "%22Jeremy+Kyle%22",
            "events": null
        }
    ],
    "as_of": "2013-07-19T09:23:13Z",
    "created_at": "2013-07-19T09:14:10Z",
    "locations": [
        {
            "name": "Worldwide",
            "woeid": 1
        }
    ]
}]

jQuery 代码

 $.getJSON("url",function(data){
   console.log(data);
});

如何从成功函数中的对象中检索值?

4

3 回答 3

2
var topTrends = [];
foreach(var i in data[0].trends) {
   var dat = data[0].trends[i].name;
   topTrends.push(dat);
   if(topTrends.length == 5) {
      return topTrends; 
   } 
} 

这将为您的事业服务

于 2013-07-19T10:42:42.040 回答
0

The twitter API sends back an object containing the trend data.

So to get each trend name it would be like below:

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

This would log all the trend names out. Hope this helps

于 2013-07-19T10:27:27.433 回答
0
var names ='';

for(var i = 0; i < data[0].trends.length; i++) {

if( i < 5){

      alert(data[0].trends[i].name);

      names += "| +"data[0].trends[i].name"";
}

}

名称 = 名称.substr(1);

于 2013-07-19T10:50:42.600 回答