javascript - 从 url 访问 JSON 中的对象时遇到问题

标签 javascript json object url getjson

如果这听起来很愚蠢,我很抱歉,但我刚刚开始学习 javascript 和 json,并且我想在完成后从 twitter API 访问 JSON 对象

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

但是,打开myjson后,找不到包含50个数组的对象image of said myjson

当我使用 $.getJSON here 时我知道该对象存在,所以现在我想知道我做错了什么,导致该对象消失了。

如果有人能指出我正确的方向,我将非常感激,因为谷歌搜索后什么也没有出现。

另外,这是我使用的 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
}

关于javascript - 从 url 访问 JSON 中的对象时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170207/

相关文章:

javascript - JavaScript 函数是对象吗?

c++ - 对于虚函数,在声明为虚函数和未声明为虚函数时访问某些方法时会发生什么

javascript - React - 一次显示一个数组值并通过单击进行更改

javascript - 具有多个参数的 knockout 点击绑定(bind)

php - MySQL 查询按 JSON 列中的值排序

json - DevOps Pipeline 在将变量值作为 JSON 正文发送以使用 Terraform 部署 Azure 资源时更改时间格式

php - 在 php/laravel 中将数据存储为对象而不是数组

javascript - 为什么/什么时候 $.inArray 返回 -1?

javascript - HTML5 DataList 搜索可用语言?

python - 使用 django 从外部数据库中提取数据到模板