从JSON对象获取select值

从JSON对象获取select值,json,Json,我想返回twitter用户的followers\u计数。当我向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:\\/\\

我想返回twitter用户的followers\u计数。当我向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\u count键值对。因为这是我唯一需要的部分。之后我可能会选择其他人,但这是主要部分。 我试过做一些类似的事情:

console.log(response.body['followers_count']);
但这是行不通的。

简单的回答是:

var responseObject = JSON.parse(response);
console.log(responseObject[0].followers_count)

请注意,上面作为响应粘贴的字符串不是有效的JSON,但我假设它是copy/paste中的错误。您可以使用来测试它。

代码>响应[0]。追随者计数这会起作用。

你解析过json吗?哪里定义了响应?
var参数={screen_name:'twitter'};get('users/lookup',params,function(error,tweets,response){if(error){console.log(error);}else{console.log(“hello”);console.log();var responseObject=JSON.parse(response);console.log(responseObject);//原始响应对象。})
Ye我试着放入一个JSON验证器,但它不正确,这是twitter API返回的结果。当我尝试上面的代码时,我得到了未定义的:1[object object]意外标记o。我不知道twitter为什么返回无效的json。我从我的命令行复制并粘贴了响应,这可能是为什么?我认为它试图解析一个对象和[正在放弃它吗?只要响应不是有效的JSON,它就永远不会工作。现在,我怀疑Twitter API是否真的返回了无效的JSON,所以我猜它没有在您的代码中正确转义/处理。发现问题出在我的能力范围之外,我只是回答了您最初的问题:-)是的,抱歉,我错过了JSON的一部分,事实上有不同的部分。保存数据的部分包含在主体中。