繁体   English   中英

从JSON Twitter响应(Tweepy和Python)中提取单个True / False值

[英]Extract single True/False value from JSON Twitter response ( Tweepy and Python )

我正在尝试在应用程序中编写一些代码,以检查用户是否关注您。 我只希望代码返回可以放入if循环中的True / False值。

代码是:

user_id = '1234567890'
print api.show_friendship(target_id=user_id)

它返回所有这些JSON,我一无所知,但希望第二个数组中的'false'值位于'following'下(对于另一个用户)(或者,在'followed_by'下的第一个数组中为true / false)。 。两种方法都可以!):

{"relationship":{"source":{"id":0000000000,"id_str":"0000000000","screen_name":"auth_user","following":false,"followed_by":false,"following_received":false,"following_requested":false,"notifications_enabled":false,"can_dm":false,"blocking":false,"blocked_by":false,"muting":false,"want_retweets":false,"all_replies":false,"marked_spam":false},"target":{"id":123456789,"id_str":"123456789","screen_name":"other_user","following":false,"followed_by":false,"following_received":false,"following_requested":false}}}

如何使我的代码为“ following”:false部分返回true / false?

(或者,第一个数组中的“ followed_by”:false部分-每个数组均来自用户关系的“观点”)

很好,发布后立即解决了。

尝试:

print json.loads(api.show_friendship(target_id=user_id))["relationship"]["target"]["followed_by"]

您可以通过遍历每个分支来遍历JSON数组(我相信很多人都知道大声笑,但我从未使用过JSON!)...

print api.show_friendship(target_screen_name=screen_name)[1].following

真正

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM