在jquery中读取json文件的右侧部分

时间:2014-10-01 10:16:39

标签: jquery arrays json

我试图用jquery读取这个json文件中的media_url链接。 由于某种原因,我无法到达我的页面所需的media_url。 这就是我的尝试:

console.log(data) <---- gives the whole file and works fine
console.log(data.statuses.media.media_url) <----- undefined

继承了底部附近的media_url的json文件:

{"statuses":[
{"metadata":
{"iso_language_code":"de","result_type":"recent"},
"created_at":"Wed Oct 01 09:56:09 +0000 2014","id":5.1725161318738e+17,"id_str":"517251613187379200",
"text":"Nebliger Morgen am Rubener Teich\n#popular #amazing #image #picture #capture #snap #beautiful #wow #click #love http:\/\/t.co\/VzPuln5EOE",
"source":"<a href=\"http:\/\/ifttt.com\" rel=\"nofollow\">IFTTT<\/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":2476813909,
"id_str":"2476813909",
"name":"Wow Pics...!!!",
"screen_name":"Wowpiks",
"location":"Earth",
"description":"One of the best pictures around the earth by many talented photographers from category art,science,nature,cities,celebrities,macros,animal and many more.",
"url":null,"entities":{"description":{"urls":[]}},
"protected":false,"followers_count":20,
"friends_count":14,"listed_count":0,
"created_at":"Sun May 04 12:13:33 +0000 2014",
"favourites_count":1,"utc_offset":null,
"time_zone":null,"geo_enabled":false,
"verified":false,"statuses_count":16440,"lang":"en",
"contributors_enabled":false,
"is_translator":false,
"is_translation_enabled":false,
"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\/462933740964937728\/PzG_EsID_normal.jpeg",
"profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/462933740964937728\/PzG_EsID_normal.jpeg",
"profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/2476813909\/1399207031","profile_link_color":"0084B4",
"profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333",
"profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,
"follow_request_sent":false,
"notifications":false},
"geo":null,"coordinates":null,
"place":null,"contributors":null,
"retweet_count":0,
"favorite_count":0,
"entities":{"hashtags":[{"text":"popular","indices":[33,41]},
{"text":"amazing","indices":[42,50]},
{"text":"image","indices":[51,57]},
{"text":"picture","indices":[58,66]},
{"text":"capture","indices":[67,75]},
{"text":"snap","indices":[76,81]},
{"text":"beautiful","indices":[82,92]},
{"text":"wow","indices":[93,97]},
{"text":"click","indices":[98,104]},
{"text":"love","indices":[105,110]}],
"symbols":[],
"urls":[],
"user_mentions":[],
"media":[{"id":5.1725161301541e+17,
"id_str":"517251613015412736",
"indices":[111,133],
"media_url":"http:\/\/pbs.twimg.com\/media\/By2lmf1IEAAUgWL.jpg",
"media_url_https":"https:\/\/pbs.twimg.com\/media\/By2lmf1IEAAUgWL.jpg",
"url":"http:\/\/t.co\/VzPuln5EOE","display_url":"pic.twitter.com\/VzPuln5EOE",
"expanded_url":"http:\/\/twitter.com\/Wowpiks\/status\/517251613187379200\/photo\/1",
"type":"photo","sizes":{"large":{"w":900,"h":604,"resize":"fit"}
"small":{"w":340,"h":228,"resize":"fit"},
"thumb":{"w":150,"h":150,"resize":"crop"},
"medium":{"w":600,"h":402,"resize":"fit"}}}]},"favorited":false,
"retweeted":false,"possibly_sensitive":false,"lang":"de"}],
"search_metadata":{"completed_in":0.009,"max_id":5.1725161318738e+17,
"max_id_str":"517251613187379200",
"next_results":"?max_id=517251613187379199&q=%23Image&count=1&include_entities=1",
"query":"%23Image","refresh_url":"?since_id=517251613187379200&q=%23Image&include_entities=1",
"count":1,"since_id":0,"since_id_str":"0"}} 

我希望有人知道如何解决这个问题!

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

除了缺少逗号之外,你错过了JSON(实体)中的一个父元素:

<强> data.statuses [0] .entities.media [0] .media_url

JSFiddle:http://jsfiddle.net/TrueBlueAussie/wrz903kx/1/

alert(data.statuses[0].entities.media[0].media_url);

如果您使用http://jsonformatter.curiousconcept.com/之类的工具,则可以展开层次结构并导航对象。