我无法根据需要遍历json对象

时间:2014-03-27 14:08:00

标签: javascript php 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"
  },

以下是循环遍历它并将其作为Feed的一部分显示的代码

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数组的帮助将非常感激。 谢谢

2 个答案:

答案 0 :(得分:0)

showretweets未定义。如果未定义showretweets,则不会发生任何事情,因为您的if语句期望它在那里。

displaylimitshowtweetlinksaddlinksheaderHTML也未定义。

我已经定义了所有这些东西,并改变了一些东西。对我来说很好看: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语句。

答案 1 :(得分:0)

尝试添加:

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

echo json_encode($data);