Search code examples
javascriptjsonobjecturlgetjson

Having trouble getting access to the Object in JSON from url


I am sorry if this sounds stupid, but I have just started learning javascript and json, and I want to get access to the JSON object from the twitter API after doing

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

however, after opening myjson, I can't find the object that contains 50 arrays image of said myjson

I know the object existed when I used $.getJSON here, so now I am wondering what did I do wrong such that the object has disappeared.

I would really appreciate it if someone could point me in the right directions, as nothing turned up after googling.

also, heres the twitter api that I used,

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


Solution

  • In your code sample json is not existing

    Be aware that what happen in your success function:

    function(data) {
      myjson = data;
    }
    

    is called asynchronously (when you receive the success response from the api) so if you have checked the content of myjson outside this function it can be that myjson has not be modified yet.

    whatever you want to do with this json you need to do it in this function (or call an other function from this one):

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