【问题标题】:Loop through twitter search JSON with jQuery使用 jQuery 循环浏览 Twitter 搜索 JSON
【发布时间】:2016-02-06 11:24:42
【问题描述】:

我正在尝试遍历以下 JSON 并将一些数据提取到交互式 twitter 提要中。

我尝试了很多解决方案,但没有一个可行,而且不太适合这个 JSON 结构。

我怎样才能用 jQuery 循环呢?

这是一个示例(两个对象):

{
"statuses":[
    {
        "metadata":{
            "result_type":"popular",
            "iso_language_code":"en"
        },
        "created_at":"Wed Nov 04 09:11:24 +0000 2015",
        "id":661833110036721664,
        "id_str":"661833110036721664",
        "text":"November ...\ud83c\udf55 #November #Pizza https:\/\/t.co\/sfmAMOS5dB",
        "source":"Twitter for iPhone<\/a>",
        "truncated":false,
        "in_reply_to_status_id":null,
        "in_reply_to_status_id_str":null,
        "in_reply_to_user_id":null,
        "in_reply_to_user_id_str":null,
        "in_reply_to_screen_name":null,
        "user":{
            "id":152902472,
            "id_str":"152902472",
            "name":"Yesung",
            "screen_name":"shfly3424",
            "location":"Korea",
            "description":"Super junior Yesung (Instagram) YESUNG1106 (weibo) Yesung110684",
            "url":null,
            "entities":{
                "description":{
                    "urls":[

                    ]
                }
            },
            "protected":false,
            "followers_count":3398960,
            "friends_count":47,
            "listed_count":35199,
            "created_at":"Mon Jun 07 04:50:16 +0000 2010",
            "favourites_count":556,
            "utc_offset":32400,
            "time_zone":"Seoul",
            "geo_enabled":false,
            "verified":true,
            "statuses_count":584,
            "lang":"ko",
            "contributors_enabled":false,
            "is_translator":false,
            "is_translation_enabled":true,
            "profile_background_color":"C0DEED",
            "profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
            "profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
            "profile_background_tile":false,
            "profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/641257798479687681\/kf7Oh8eI_normal.jpg",
            "profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/641257798479687681\/kf7Oh8eI_normal.jpg",
            "profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/152902472\/1424152890",
            "profile_link_color":"0084B4",
            "profile_sidebar_border_color":"C0DEED",
            "profile_sidebar_fill_color":"DDEEF6",
            "profile_text_color":"333333",
            "profile_use_background_image":true,
            "has_extended_profile":false,
            "default_profile":true,
            "default_profile_image":false,
            "following":false,
            "follow_request_sent":false,
            "notifications":false
        },
        "geo":null,
        "coordinates":null,
        "place":null,
        "contributors":null,
        "is_quote_status":false,
        "retweet_count":4451,
        "favorite_count":6201,
        "entities":{
            "hashtags":[
                {
                    "text":"November",
                    "indices":[
                        15,
                        24
                    ]
                },
                {
                    "text":"Pizza",
                    "indices":[
                        25,
                        31
                    ]
                }
            ],
            "symbols":[

            ],
            "user_mentions":[

            ],
            "urls":[

            ],
            "media":[
                {
                    "id":661833101987835905,
                    "id_str":"661833101987835905",
                    "indices":[
                        32,
                        55
                    ],
                    "media_url":"http:\/\/pbs.twimg.com\/media\/CS9NsyCUcAEZ6aU.jpg",
                    "media_url_https":"https:\/\/pbs.twimg.com\/media\/CS9NsyCUcAEZ6aU.jpg",
                    "url":"https:\/\/t.co\/sfmAMOS5dB",
                    "display_url":"pic.twitter.com\/sfmAMOS5dB",
                    "expanded_url":"http:\/\/twitter.com\/shfly3424\/status\/661833110036721664\/photo\/1",
                    "type":"photo",
                    "sizes":{
                        "large":{
                            "w":1024,
                            "h":536,
                            "resize":"fit"
                        },
                        "medium":{
                            "w":600,
                            "h":314,
                            "resize":"fit"
                        },
                        "thumb":{
                            "w":150,
                            "h":150,
                            "resize":"crop"
                        },
                        "small":{
                            "w":340,
                            "h":177,
                            "resize":"fit"
                        }
                    }
                }
            ]
        },
        "favorited":false,
        "retweeted":false,
        "possibly_sensitive":false,
        "lang":"en"
    },
    {
        "metadata":{
            "result_type":"popular",
            "iso_language_code":"en"
        },
        "created_at":"Wed Nov 04 03:57:23 +0000 2015",
        "id":661754087097040896,
        "id_str":"661754087097040896",
        "text":"I've spent seven years deleting promotional emails because one time I ordered a pizza online and I\u2019m too overwhelmed to unsubscribe.",
        "source":"Echofon<\/a>",
        "truncated":false,
        "in_reply_to_status_id":null,
        "in_reply_to_status_id_str":null,
        "in_reply_to_user_id":null,
        "in_reply_to_user_id_str":null,
        "in_reply_to_screen_name":null,
        "user":{
            "id":18948541,
            "id_str":"18948541",
            "name":"Seth MacFarlane",
            "screen_name":"SethMacFarlane",
            "location":"Los Angeles",
            "description":"The Official Twitter Page of Seth MacFarlane - new album No One Ever Tells You available now on iTunes https:\/\/t.co\/gLePVn5Mho",
            "url":"https:\/\/t.co\/o4miqWAHnW",
            "entities":{
                "url":{
                    "urls":[
                        {
                            "url":"https:\/\/t.co\/o4miqWAHnW",
                            "expanded_url":"http:\/\/www.facebook.com\/pages\/Seth-MacFarlane\/14105972607?ref=ts",
                            "display_url":"facebook.com\/pages\/Seth-Mac\u2026",
                            "indices":[
                                0,
                                23
                            ]
                        }
                    ]
                },
                "description":{
                    "urls":[
                        {
                            "url":"https:\/\/t.co\/gLePVn5Mho",
                            "expanded_url":"http:\/\/itun.es\/us\/Vx9p-",
                            "display_url":"itun.es\/us\/Vx9p-",
                            "indices":[
                                103,
                                126
                            ]
                        }
                    ]
                }
            },
            "protected":false,
            "followers_count":8155852,
            "friends_count":336,
            "listed_count":32067,
            "created_at":"Tue Jan 13 19:04:37 +0000 2009",
            "favourites_count":0,
            "utc_offset":-28800,
            "time_zone":"Pacific Time (US & Canada)",
            "geo_enabled":false,
            "verified":true,
            "statuses_count":5087,
            "lang":"en",
            "contributors_enabled":false,
            "is_translator":false,
            "is_translation_enabled":true,
            "profile_background_color":"C0DEED",
            "profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
            "profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
            "profile_background_tile":false,
            "profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/477598819715395585\/g0lGqC_J_normal.jpeg",
            "profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/477598819715395585\/g0lGqC_J_normal.jpeg",
            "profile_link_color":"0084B4",
            "profile_sidebar_border_color":"C0DEED",
            "profile_sidebar_fill_color":"DDEEF6",
            "profile_text_color":"333333",
            "profile_use_background_image":true,
            "has_extended_profile":false,
            "default_profile":true,
            "default_profile_image":false,
            "following":false,
            "follow_request_sent":false,
            "notifications":false
        },
        "geo":null,
        "coordinates":null,
        "place":null,
        "contributors":null,
        "is_quote_status":false,
        "retweet_count":1204,
        "favorite_count":3895,
        "entities":{
            "hashtags":[

            ],
            "symbols":[

            ],
            "user_mentions":[

            ],
            "urls":[

            ]
        },
        "favorited":false,
        "retweeted":false,
        "lang":"en"
    },
]
}

【问题讨论】:

    标签: javascript jquery json twitter


    【解决方案1】:

    你可以试试这样的。

    var data = {} // your json data 
    

    在您的情况下,它包含两个节点,即 - data.statuses[0]data.statuses[1] 然后遍历每个以获取所有属性。

         $.each(data.statuses[0], function(key,valueObj){
                    console.log(key + "/" + valueObj );
                });
    
        /*repeat it for data.statuses[1] as well*/
    

    如果您还想要嵌套数据,则在具有相同 jQuery.each() 函数的同一循环内放置一个循环。

    你也可以用纯 Javascript 来做。

    var dataVal = data.statuses[1];
    for(var prop in dataVal){
       if(dataVal.hasOwnProperty(prop)){
         // do something else with dataVal[prop]
       }
    }
    

    【讨论】:

      【解决方案2】:

      我使用下划线来模板化数据,基本上我已经抓取了您的数据并将其传递到模板中,然后我使用下划线each 实用程序来循环状态。

      这是一个模板示例

      <script type="text/html" id="tmpl">
          <% _.each(statuses, function(status) { %>
              <div class="user">
                  <p>User: <%= status.user.name %></p>
                  <p>Screen Name: <%= status.user.screen_name %></p>
                  <p>Location: <%= status.user.location %></p>
              </div>
          <% }) %>
      
      </script>
      

      还有一个可以帮助你的小提琴http://jsfiddle.net/kyllle/6Lupe1en/

      【讨论】:

        猜你喜欢
        • 2018-08-12
        • 2011-09-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 2012-08-21
        • 2012-10-21
        • 2014-05-04
        相关资源
        最近更新 更多