2014-10-01 3 views
0

Im пытается прочитать ссылку media_url в этом json-файле с jquery. по какой-то причине я не могу достичь media_url, который мне нужен для моей страницы. это то, что я пробовал:Прочитайте правую часть json-файла в jQuery

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

Heres файл в формате JSON с MEDIA_URL близко к дну:

{"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"}} 

Я надеюсь, что кто-то знает, как это исправить!

Спасибо за помощь!

+0

Проверка вашего json не удалась. У вас есть запятая, отсутствующая после «размеров»: {«большой»: {«w»: 900, «h»: 604, «изменить размер»: «подходит»}. Как только он будет проверяться, вы увидите, есть ли у вас такая же проблема. – haxtbh

+0

Используйте этот http://json.parser.online.fr/, чтобы разобрать ваш JSON, у него есть ошибка. –

+0

После того, как вы исправили строку 'JSON', обратите внимание, что' statuses' является массивом, поэтому вы не можете напрямую обращаться к элементу 'media', вы должны выбрать элемент из массива и затем попытаться получить доступ к' media' property 'console .log (data.statuses [0] .media.media_url) ' – Michael

ответ

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/ вы можете расширить иерархию и перемещения объектов.

Смежные вопросы