在Google App Script中解析Twitter JSON时出现问题

6xfqseft  于 5个月前  发布在  Go
关注(0)|答案(1)|浏览(72)

我正在使用GAS documentation中描述的代码来检索我的Twitter时间轴。

var response = twitterService.fetch('https://api.twitter.com/1.1/statuses/home_timeline.json?count=1');
 response=response.getContentText();

字符串
产生以下结果:

[{"created_at":"Sun Jan 14 15:30:00 +0000 2018","id":952563458616172544,"id_str":"952563458616172544","text":"#UltimOra, \u201cAi piedi le Clarks\u201d; #LiberieUguali trova il primo punto di convergenza fra i vari leader https:\/\/omissis","truncated":false,"entities":{"hashtags":[{"text":"UltimOra","indices":[0,9]},{"text":"LiberieUguali","indices":[33,47]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/omissis","expanded_url":"http:\/\/www.lercio.it\/ai-piedi-le-clarks-liberi-e-uguali-trova-il-primo-punto-di-convergenza-fra-i-vari-leader\/","display_url":"lercio.it\/ai-piedi-le-cl\u2026","indices":[102,125]}]},"source":"\u003ca href=\"https:\/\/about.twitter.com\/products\/tweetdeck\" rel=\"nofollow\"\u003eTweetDeck\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,"user":{"id":910827588,"id_str":"910827588","name":"lercio-notizie","screen_name":"lercionotizie","location":"","description":"Dal 13 aprile 2017 in fumetteria e libreria con \"Lercio, lo sporco che fa notizia. Il libro\" (Shockdom)","url":"http:\/\/omissis","entities":{"url":{"urls":[{"url":"http:\/\/omissis","expanded_url":"http:\/\/www.lercio.it","display_url":"lercio.it","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":491614,"friends_count":25,"listed_count":712,"created_at":"Sun Oct 28 18:19:33 +0000 2012","favourites_count":385,"utc_offset":3600,"time_zone":"Amsterdam","geo_enabled":false,"verified":false,"statuses_count":7335,"lang":"it","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"FFFFFF","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/696203701\/001bc779080491195bc16054af629016.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/696203701\/001bc779080491195bc16054af629016.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/2932719853\/4bcd23b39293870bf1832aec72b6fe3d_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/2932719853\/4bcd23b39293870bf1832aec72b6fe3d_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/910827588\/1467828171","profile_link_color":"9B1212","profile_sidebar_border_color":"000000","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":true,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":73,"favorite_count":186,"favorited":false,"retweeted":false,"possibly_sensitive":false,"possibly_sensitive_appealable":false,"lang":"it"}]


但当我添加代码行时,

response=JSON.parse(response);


JSON文件被完全改变了,它甚至看起来不像JSON文件,它的内容变成了:

{possibly_sensitive_appealable=false, in_reply_to_status_id_str=null, in_reply_to_status_id=null, created_at=Sun Jan 14 15:30:00 +0000 2018, in_reply_to_user_id_str=null, source=<a href="https://about.twitter.com/products/tweetdeck" rel="nofollow">TweetDeck</a>, retweet_count=73, retweeted=false, geo=null, in_reply_to_screen_name=null, is_quote_status=false, id_str=952563458616172544, in_reply_to_user_id=null, favorite_count=186, id=9.5256345861617254E17, text=#UltimOra, “Ai piedi le Clarks”; #LiberieUguali trova il primo punto di convergenza fra i vari leader https://omissis, place=null, lang=it, favorited=false, possibly_sensitive=false, coordinates=null, truncated=false, entities={urls=[Ljava.lang.Object;@7d41de2d, hashtags=[Ljava.lang.Object;@5e05507d, user_mentions=[Ljava.lang.Object;@7b5572f2, symbols=[Ljava.lang.Object;@fb373c9}, contributors=null, user={utc_offset=3600, friends_count=25, profile_image_url_https=https://pbs.twimg.com/profile_images/2932719853/4bcd23b39293870bf1832aec72b6fe3d_normal.png, listed_count=712, profile_background_image_url=http://pbs.twimg.com/profile_background_images/696203701/001bc779080491195bc16054af629016.png, default_profile_image=false, favourites_count=385, description=Dal 13 aprile 2017 in fumetteria e libreria con "Lercio, lo sporco che fa notizia. Il libro" (Shockdom), created_at=Sun Oct 28 18:19:33 +0000 2012, is_translator=false, profile_background_image_url_https=https://pbs.twimg.com/profile_background_images/696203701/001bc779080491195bc16054af629016.png, protected=false, screen_name=lercionotizie, id_str=910827588, profile_link_color=9B1212, is_translation_enabled=false, translator_type=none, id=910827588, geo_enabled=false, profile_background_color=FFFFFF, lang=it, has_extended_profile=false, profile_sidebar_border_color=000000, profile_text_color=333333, verified=false, profile_image_url=http://pbs.twimg.com/profile_images/2932719853/4bcd23b39293870bf1832aec72b6fe3d_normal.png, time_zone=Amsterdam, url=http://omissis, contributors_enabled=false, profile_background_tile=false, profile_banner_url=https://pbs.twimg.com/profile_banners/910827588/1467828171, entities={description={urls=[Ljava.lang.Object;@7c7074a4}, url={urls=[Ljava.lang.Object;@80245ef}}, statuses_count=7335, follow_request_sent=false, followers_count=491614, profile_use_background_image=true, default_profile=false, following=true, name=lercio-notizie, location=, profile_sidebar_fill_color=DDEEF6, notifications=false}}


你怎么了?

aydmsdu9

aydmsdu91#

您可能正在使用Logger.log来检查代码执行的结果。
如文件所述:
方法Logger.log需要一个字符串值,但是如果你传入一个 objectarray*,它会尽最大努力将其转换为字符串。
因此,response在这种情况下,response=response.getContentText()将给予您在Logger.log中使用时的预期行为,但在response=JSON.parse(response)将给予Object的情况下,将以Logger.log输出
[key1=val1, key2=val2, ..]**。
你可以习惯它,或者你可以在记录它之前使用对象到字符串的转换:
Logger.log(JSON.stringify(response))

相关问题