0

これはJSONの出力です

{
  "created_at": "Wed, 09 May 2012 22:13:32 +0000",
  "from_user": "EastCClothing",
  "from_user_id": 119699125,
  "from_user_id_str": "119699125",
  "from_user_name": "Tester23",
  "geo": {
    "coordinates": [
      54.9742,
      -1.5974
    ],
    "type": "Point"
  },
  "id": 200347799861198850,
  "id_str": "200347799861198849",
  "iso_language_code": "pt",
  "metadata": {
    "result_type": "recent"
  },
  "profile_image_url": "http://a0.twimg.com/sticky/default_profile_images/default_profile_0_normal.png",
  "profile_image_url_https": "https://si0.twimg.com/sticky/default_profile_images/default_profile_0_normal.png",
  "source": "<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>",
  "text": "#CM0655 GEO",
  "to_user": null,
  "to_user_id": 0,
  "to_user_id_str": "0",
  "to_user_name": null
}, 

私がやろうとしているのは、GoogleマップAPIで使用できるように座標を取得することです.他のすべての詳細には問題なくアクセスできますが、期待した「json.results [ i].geo.coordinates'. これが私のJQueryです

$(function(){
    function searchTweets() {
        var url='http://search.twitter.com/search.json?callback=?&q=cm0655';
        $.getJSON(url,function(json){


            var output = [];


            for (var i = 0, len = json.results.length; i < len; i++) {


               output.push('<p id="tweet"><img id="tweetImage" src="' + json.results[i].profile_image_url + '" width="48" height="48" /><span id="tweetText">' + '<a href="http://twitter.com/' + json.results[i].from_user + '">' + json.results[i].from_user + "</a> " + json.results[i].text + "<br />" + json.results[i].created_at + '</p>' + json.results[i].geo.coordinates + '</span>');
            }

            $("div.twitter2").html(output.join('')).slideDown('slow');
        });
    }


    var timer = setInterval(searchTweets, 20000);


    searchTweets();

});

どうもありがとう

4

3 に答える 3

2

誰かがその道を進んだ場合に備えて、$。eachメソッドを追加すると思いました。

    // The code below uses the Twitter API found here https://dev.twitter.com/docs to search for the latest mention of CM0655 (module code).

    var twitterURL = "http://search.twitter.com/search.json?q=%23CM0655";
    function twitterSearch() { // Create the weather function   
        $.ajax({
        url: twitterURL, //http request string for the weather service
        dataType: "jsonp",                                                                                                  
        success: function(JSON) {                      // If successful parse the JSON data
            $('#twitterSearch').html("");
            $.each(JSON.results, function(i,tweet){
                    var tweeterID  = tweet.from_user_id;
                    var dateFormat = tweet.created_at;
                    var newDate    = dateFormat.replace('+0000', '');
                    var title      = 'title="Tweeted from"';
                    var id         = tweet.id;
                    var locData    = tweet.geo;
                    getCordsData(locData);
                    $('#twitterSearch').append('<li id="tweet' + id + '" class="mainP tweet"><img class="tweetImage" src="'+ tweet.profile_image_url +'" height="20" width="20" /> <a class="tweetLink" href="http://twitter.com/' + tweet.from_user + '" target="_blank">' + tweet.from_user + '</a> on the ' + newDate + ' <br /> tweeted<span id="tweetTextColor">: ' + tweet.text + '</span></li>');
            });
        //alert("Ajax text");
        setTimeout(twitterSearch, 10000);
        }
        }); // End of ajax
     } // End of function

    function getCordsData(data){
        if(data == null){
            data = "No location data found!";
            alert(data);
        }else{
            var long = data['coordinates'][0];
            var lat = data['coordinates'][1];
            alert("long:" + long + "Lat:" + lat);
        }
    }
于 2012-05-12T09:29:59.613 に答える
1

以前の失敗で申し訳ありませんが、解決策を見つけました

免責事項 これは非常に最善の解決策ではありませんが、問題を解決する方法のアイデアを提供するのに役立つものです。それが役に立てば幸い!

$(function() {
    function searchTweets() {
        var url = 'http://search.twitter.com/search.json?callback=?&q=from:Eastcclothing';
        $.getJSON(url, function(json) {

            console.log(json);
            var output = [];


            for (var i = 0, len = json.results.length; i < len; i++) {

                if ($.isPlainObject(json.results[i].geo)) {
                    output.push('<p id="tweet"><img id="tweetImage" src="' + json.results[i].profile_image_url + '" width="48" height="48" /><span id="tweetText">' + '<a href="http://twitter.com/' + json.results[i].from_user + '">' + json.results[i].from_user + "</a> " + json.results[i].text + "<br />" + json.results[i].created_at + '</p>' + json.results[i].geo.coordinates + '</span>');
                } else {
                    output.push('<p id="tweet"><img id="tweetImage" src="' + json.results[i].profile_image_url + '" width="48" height="48" /><span id="tweetText">' + '<a href="http://twitter.com/' + json.results[i].from_user + '">' + json.results[i].from_user + "</a> " + json.results[i].text + "<br />" + json.results[i].created_at + '</p>' + json.results[i].geo + '</span>');
                }
            }

            $("div.twitter2").html(output.join('')).slideDown('slow');
        });
    }


    var timer = setInterval(searchTweets, 20000);


    searchTweets();

});​

また、動作中のjsfiddle http://jsfiddle.net/th3fallen/jc2Kf/を支援するために

于 2012-05-10T19:20:36.290 に答える
0

配列を文字列と連結しようとしているため、問題が発生していると思います。必要な場合は次のとおりです。

...+"["+json.results[i].geo.coordinates[0]+","+json.results[i].geo.coordinates[1]+"]"+...

だけではなく

...+json.results[i].geo.coordinates+...

またはこれを試してください:

if (json.results[i].geo) console.log('<p id="tweet"><img id="tweetImage" src="' + json.results[i].profile_image_url + '" width="48" height="48" /><span id="tweetText">' + '<a href="http://twitter.com/' + json.results[i].from_user + '">' + json.results[i].from_user + "</a> " + json.results[i].text + "<br />" + json.results[i].created_at + '</p>' + "["+json.results[i].geo.coordinates[0]+","+json.results[i].geo.coordinates[1]+"]" + '</span>');

あるいは:

if (json.results[i].geo) console.log($('<p id="tweet"><img id="tweetImage" src="' + json.results[i].profile_image_url + '" width="48" height="48" /><span id="tweetText">' + '<a href="http://twitter.com/' + json.results[i].from_user + '">' + json.results[i].from_user + "</a> " + json.results[i].text + "<br />" + json.results[i].created_at + '</p>' + "["+json.results[i].geo.coordinates[0]+","+json.results[i].geo.coordinates[1]+"]" + '</span>'));

html が正しく表示されていることを確認します。

私が気付いた他の問題は、有効な HTML ではないタグの</span>前にタグを閉じていることです。</p>それがあなたの問題だとは思いませんが(そうかもしれませんか?)、おそらく修正する必要があります。

于 2012-05-10T16:41:55.810 に答える