我想要返回twitter用户的followers_count。当我向api发出get请求时,我返回的是JSON:
编辑:响应有多个部分我希望从开头提取数据的部分如下:
body:'[{"id":842072275,"id_str":"842072275","name":"BreaK","screen_name":"BreaK_71","location":"England","description":"Full Time Streamer from the UK","url":"http:\\/\\/t.co\\/YZTfi5U7AE","entities":{"url":{"urls":[{"url":"http:\\/\\/t.co\\/YZTfi5U7AE","expanded_url":"http:\\/\\/www.twitch.tv\\/break71","display_url":"twitch.tv\\/break71","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":4159,"friends_count":211,"listed_count":43,"created_at":"Sun Sep 23 17:13:19 +0000 2012","favourites_count":4262,"utc_offset":7200,"time_zone":"Amsterdam","geo_enabled":false,"verified":false,"statuses_count":9763,"lang":"en","status":{"created_at":"Fri Apr 08 17:46:52 +0000 2016","id":718495339188195328,"id_str":"718495339188195328","text":"Hey I\'m live with some @BattleRoyaleMod talking hype about the Tournament tomorrow and messing around. Lets gooo! https:\\/\\/t.co\\/4V9CHaTcIK","entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"BattleRoyaleMod","name":"PLAYERUNKNOWN","id":1880574811,"id_str":"1880574811","indices":[23,39]}],"urls":[{"url":"https:\\/\\/t.co\\/4V9CHaTcIK","expanded_url":"http:\\/\\/twitch.tv\\/break71","display_url":"twitch.tv\\/break71","indices":[114,137]}]},"truncated":false,"source":"\\u003ca href=\\"http:\\/\\/twitter.com\\" rel=\\"nofollow\\"\\u003eTwitter Web Client\\u003c\\/a\\u003e","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,"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":3,"favorite_count":5,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},"contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"1A1B1F","profile_background_image_url":"http:\\/\\/pbs.twimg.com\\/profile_background_images\\/642608935812374528\\/7t7IqgIJ.png","profile_background_image_url_https":"https:\\/\\/pbs.twimg.com\\/profile_background_images\\/642608935812374528\\/7t7IqgIJ.png","profile_background_tile":false,"profile_image_url":"http:\\/\\/pbs.twimg.com\\/profile_images\\/693388490642423808\\/vdfXvyl4_normal.png","profile_image_url_https":"https:\\/\\/pbs.twimg.com\\/profile_images\\/693388490642423808\\/vdfXvyl4_normal.png","profile_banner_url":"https:\\/\\/pbs.twimg.com\\/profile_banners\\/842072275\\/1410269304","profile_link_color":"ED7811","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"F6FFD1","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false}]' }
如何仅提取followers_count键值对。因为这是我需要的唯一部分。我可能会选择其他人,但这是主要部分。 我尝试过这样的事情:
console.log(response.body['followers_count']);
但那不起作用。
答案 0 :(得分:0)
简短回答是:
var responseObject = JSON.parse(response);
console.log(responseObject[0].followers_count)
请注意,您在上面粘贴的字符串作为响应不是有效的JSON,但我认为它是复制/粘贴中的错误。您可以使用online json validator对其进行测试。
答案 1 :(得分:0)
response[0].followers_count;
这会有用。