-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

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 に答える
0

Twitter API は、トレンド データを含むオブジェクトを返します。

したがって、各トレンド名を取得するには、次のようになります。

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

これにより、すべてのトレンド名がログに記録されます。お役に立てれば

于 2013-07-19T10:27:27.433 に答える