簡體   English   中英

無法從url訪問JSON中的Object

[英]Having trouble getting access to the Object in JSON from url

我很抱歉,如果這聽起來很愚蠢,但我剛剛開始學習javascript和json,我想在做完后從twitter API訪問JSON對象

 var myjson; $.getJSON('url of the Object', function(data) { myjson = json; }); 

然而,在打開myjson后,我找不到包含50個數組的對象, 表示myjson

我知道在這里使用$ .getJSON時存在的對象,所以現在我想知道我做錯了什么,以至於對象已經消失了。

如果有人可以指出我正確的方向,我會非常感激,因為谷歌搜索后沒有出現任何問題。

還有,我使用的twitter api,

 {"statuses":[{"created_at":"Sun Jul 03 13:03:21 +0000 2016","id":749589344554459137,"id_str":"749589344554459137","text":"RT @Pontifex: Loving and forgiving as God loves and forgives. This is a programme of life that can know no interruptions or exceptions.","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"Pontifex","name":"Pope Francis","id":500704345,"id_str":"500704345","indices":[3,12]}],"urls":[]},"metadata":{"iso_language_code":"en","result_type":"recent"},"source":"<a href=\\ "http:\\/\\/twitter.com\\/download\\/android\\" rel=\\ "nofollow\\">Twitter for Android<\\/a>","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":2656789554,"id_str":"2656789554","name":"Augustine Neto T","screen_name":"NetoTumwebaze","location":"At The Heart Of Africa","description":"Human Resources Officer - MTN Rwanda","url":"http:\\/\\/t.co\\/zzdpXkfiue","entities":{"url":{"urls":[{"url":"http:\\/\\/t.co\\/zzdpXkfiue","expanded_url":"http:\\/\\/www.mtn.co.rw","display_url":"mtn.co.rw","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":153,"friends_count":148,"listed_count":13,"created_at":"Fri Jul 18 14:03:38 +0000 2014","favourites_count":15,"utc_offset":10800,"time_zone":"Kyiv","geo_enabled":false,"verified":false,"statuses_count":3843,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"000000","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\\/729685855565185024\\/KcmSmNA6_normal.jpg","profile_image_url_https":"https:\\/\\/pbs.twimg.com\\/profile_images\\/729685855565185024\\/KcmSmNA6_normal.jpg","profile_banner_url":"https:\\/\\/pbs.twimg.com\\/profile_banners\\/2656789554\\/1418826532","profile_link_color":"89C9FA","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Jul 03 12:00:08 +0000 2016","id":749573436637261824,"id_str":"749573436637261824","text":"Loving and forgiving as God loves and forgives. This is a programme of life that can know no interruptions or exceptions.","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"metadata":{"iso_language_code":"en","result_type":"recent"},"source":"<a href=\\"https:\\/\\/about.twitter.com\\/products\\/tweetdeck\\" rel=\\"nofollow\\">TweetDeck<\\/a>","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":500704345,"id_str":"500704345","name":"Pope Francis","screen_name":"Pontifex","location":"Vatican City","description":"Welcome to the official Twitter page of His Holiness Pope Francis","url":"http:\\/\\/t.co\\/MlfhXffVrl","entities":{"url":{"urls":[{"url":"http:\\/\\/t.co\\/MlfhXffVrl","expanded_url":"http:\\/\\/www.news.va","display_url":"news.va","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":9506393,"friends_count":8,"listed_count":24637,"created_at":"Thu Feb 23 11:38:07 +0000 2012","favourites_count":0,"utc_offset":7200,"time_zone":"Rome","geo_enabled":false,"verified":true,"statuses_count":879,"lang":"it","contributors_enabled":false,"is_translator":false,"is_translation_enabled":true,"profile_background_color":"FFFFFF","profile_background_image_url":"http:\\/\\/pbs.twimg.com\\/profile_background_images\\/857999332\\/52f4b5a0278e5a44ed595ef3a031a709.jpeg","profile_background_image_url_https":"https:\\/\\/pbs.twimg.com\\/profile_background_images\\/857999332\\/52f4b5a0278e5a44ed595ef3a031a709.jpeg","profile_background_tile":false,"profile_image_url":"http:\\/\\/pbs.twimg.com\\/profile_images\\/507818066814590976\\/KNG-IkT9_normal.jpeg","profile_image_url_https":"https:\\/\\/pbs.twimg.com\\/profile_images\\/507818066814590976\\/KNG-IkT9_normal.jpeg","profile_banner_url":"https:\\/\\/pbs.twimg.com\\/profile_banners\\/500704345\\/1409908226","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"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":2679,"favorite_count":5015,"favorited":false,"retweeted":false,"lang":"en"},"is_quote_status":false,"retweet_count":2679,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"en"}],"search_metadata":{"completed_in":0.032,"max_id":749589344554459137,"max_id_str":"749589344554459137","query":"life","refresh_url":"?since_id=749589344554459137&q=life&include_entities=1","count":2,"since_id":0,"since_id_str":"0"}} 

在您的代碼示例中,json不存在

請注意,您的成功功能會發生什么:

function(data) {
  myjson = data;
}

異步調用(當你從api收到成功響應時),所以如果你在這個函數之外檢查了myjson的內容,那么myjson還沒有被修改。

無論你想用這個json做什么,你需要在這個函數中做到這一點(或者從這個函數調用另一個函數):

function(data) {
  myjson = data;
  //do some stuff with myjson
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM