2014-03-27 37 views
0

我打算循環瀏覽json對象,將對象的屬性分配給變量,然後顯示它們。我相信問題在於我對json進行了格式化。我無法根據需要循環瀏覽json對象

這裏是我的代碼來創建對象

$data = array(); 
while($row = mysql_fetch_array($sql)){ 
    $row_data = array(
    'name' => $row['name'], 
    'user' => $row['user'], 
    'status' => $row['status'], 
    'id' => $row['id'], 
     'img' => $row['Img'] 
    ); 
    array_push($data, $row_data); 
} 

echo json_encode($data); 

這裏是JSON

[ 
    { 
    "name": "Adrian Connolly", 
    "user": "NotRockysWife", 
    "status": "Big thank you to Fiona in @CarphoneIE Patrick St Cork for all her help sorting my wifes broken iphone,quality service,will shop there again\ud83d\ude03", 
    "id": "4551", 
    "img": "https:\/\/pbs.twimg.com\/profile_images\/442595842994995200\/DQVTCx4M_normal.jpeg" 
    }, 
    { 
    "name": "Ali B", 
    "user": "Ms_Ali_B", 
    "status": "@killianbyrne my first iPhone (from NYC jail broken) was a 4gb one. I also dropped it down the loo when it was 3 months old. :-(", 
    "id": "4552", 
    "img": "https:\/\/pbs.twimg.com\/profile_images\/441908051776258048\/ekyyTPaH_normal.jpeg" 
    }, 
    { 
    "name": "The Running Geek", 
    "user": "welshgje", 
    "status": "@BBCiPlayer radio seems to have broken. Not connecting on iphone. Anyone else?", 
    "id": "4553", 
    "img": "https:\/\/pbs.twimg.com\/profile_images\/378800000829569378\/1897ea7fafffc73b80aa39e8714c65b2_normal.jpeg" 
    }, 
    { 
    "name": "Sa\u00f3irse McAuley", 
    "user": "saoirsemcauley1", 
    "status": "Phones working again but feck that.. Going to treat myself to new iphone after work for my hols! Dont want a broken one \ud83d\ude12", 
    "id": "4554", 
    "img": "https:\/\/pbs.twimg.com\/profile_images\/431472763384365057\/g49s5WU__normal.jpeg" 
    }, 

這裏的樣本,通過它循環,並顯示其作爲飼料的一部分代碼

function gettwitterjson() { 
      $.getJSON('http://localhost/4th/twitdb2/js/fetch.php', 
       function(data) { 

        var feedHTML = ''; 
        var displayCounter = 1; 
        for (var i=0; i<data.length; i++) { 
         var tweetscreenname = data[i].name; 
         var tweetusername = data[i].user; 
         var profileimage = data[i].img; 
         var status = data[i].status; 
         var isaretweet = false; 
         var isdirect = false; 
         var tweetid = data[i].id; 





         if (((showretweets == true) || ((isaretweet == false) && (showretweets == false)))) { 
          if ((data[i].text.length > 1) && (displayCounter <= displaylimit)) {    
           if (showtweetlinks == true) { 
            status = addlinks(status); 
           } 

           if (displayCounter == 1) { 
            feedHTML += headerHTML; 
           } 

           feedHTML += '<div class="twitter-article" id="tw'+displayCounter+'">';     
           feedHTML += '<div class="twitter-pic"><a href="https://twitter.com/'+tweetusername+'" ><img src="'+profileimage+'"images/twitter-feed-icon.png" width="125" height="125" alt="twitter icon" /></a></div>'; 
           feedHTML += '<div class="twitter-text"><p><span class="tweetprofilelink"><strong><a href="https://twitter.com/'+tweetusername+'" >'+tweetscreenname+'</a></strong> <a href="https://twitter.com/'+tweetusername+'" >@'+tweetusername+'</a></span><span class="tweet-time"><a href="https://twitter.com/'+tweetusername+'/status/'+tweetid+'"></a></span><br/>'+status+'</p></div>'; 




          if ((isaretweet == true) && (showretweetindicator == true)) { 
            feedHTML += '<div id="retweet-indicator"></div>'; 
           }      
           if (showtweetactions == true) { 
           feedHTML += '<div id="twitter-actions"><div class="intent" id="intent-reply"><a href="https://twitter.com/intent/tweet?in_reply_to='+tweetid+'" title="Reply"></a></div><div class="intent" id="intent-retweet"><a href="https://twitter.com/intent/retweet?tweet_id='+tweetid+'" title="Retweet"></a></div><div class="intent" id="intent-fave"><a href="https://twitter.com/intent/favorite?tweet_id='+tweetid+'" title="Favourite"></a></div></div>'; 
           } 
           feedHTML += '</div>'; 
           displayCounter++; 
          } 
         } 
        } 

        $('#twitter-feed').html(feedHTML); 



        //Add twitter action animation and rollovers 
        if (showtweetactions == true) {    
         $('.twitter-article').hover(function(){ 
          $(this).find('#twitter-actions').css({'display':'block', 'opacity':0, 'margin-top':-20}); 
          $(this).find('#twitter-actions').animate({'opacity':1, 'margin-top':0},200); 
         }, function() { 
          $(this).find('#twitter-actions').animate({'opacity':0, 'margin-top':-20},120, function(){ 
           $(this).css('display', 'none'); 
          }); 
         });   

         //Add new window for action clicks 

         $('#twitter-actions a').click(function(){ 
          var url = $(this).attr('href'); 
          window.open(url, 'tweet action window', 'width=580,height=500'); 
          return false; 
         }); 
        } 
      }); 
     } 

我相信問題在於我已經制定了json對象。任何有關如何以不同方式創建json數組的幫助將非常感謝。 謝謝

+0

是這段代碼的PHP?第一部分是 –

+0

。我關心的部分是json對象 – user3347956

+1

JavaScript向控制檯拋出什麼錯誤? 您是否在功能啓動時檢查了「數據」的值(例如,使用console.log(數據)? – geekymartian

回答

0

showretweets未定義。當沒有定義showretweets時,什麼都不會發生,因爲你的if語句期望它在那裏。

displaylimitshowtweetlinks,addlinksheaderHTML也未定義。

我已經定義了所有這些東西,並且改變了一些東西。看起來好像沒什麼問題:http://jsfiddle.net/9s7rv/


我改變

if ((data[i].text.length 

if ((data[i].status.length 

因爲.text沒有定義。我只是採取了我最好的猜測。


我改變

if (((showretweets == true) || ((isaretweet == false) && (showretweets == false)))) { 

if (showretweets || (!isaretweet && !showretweets)) { 

,同樣爲所有其他if語句。

0

嘗試增加:

Header('content-type:application/json'); 

echo json_encode($data);