使用jQuery循环浏览twitter搜索JSON
Loop through twitter search JSON with jQuery
我正试图遍历下面的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"
},
]
}
您可以尝试这样的方法。
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]
}
}
我已经使用下划线来对数据进行模板化,基本上我已经获取了您的数据并将其传递到模板中,然后我使用下划线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/
相关文章:
- 在禁用ng的情况下搜索JSON对象(AngularJS)
- 使用ng类(AngularJS)搜索JSON对象
- 自动完成功能,用于按属性搜索 json 对象
- 如何搜索 json 结构
- 如何在jQuery中搜索JSON
- 搜索 JSON 数组
- 根据变量搜索 JSON 数组
- 递归搜索 JSON 并删除某些子对象
- 使用jQuery提高搜索JSON对象的性能
- 将Multiple Var输入函数,搜索JSON数组,并将Multiple Array Val作为输出接收
- 用于搜索JSON数据的自定义筛选器
- 在JavaScript中搜索Json
- 如何在JavaScript中搜索JSON数组中的特定参数
- 使用jQuery循环浏览twitter搜索JSON
- 搜索JSON以查找特定值
- TypeAhead和Bloodhound只搜索JSON数组中的第一个单词
- 使用JQuery搜索JSON对象
- 使用从下拉菜单输入的用户日期搜索JSON
- 在ember.js中搜索JSON数据
- 二进制搜索JSON对象