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



我正试图在一个应用程序中编写一点代码,以检查用户是否在关注你。我只想让代码返回一个True/False值,我可以把它放在if循环中。

代码为:

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

它返回所有这些JSON,我对此一无所知,但希望在"following"下为另一个用户从第二个数组中获取"false"值(或者,在"follow ed_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"的true/false: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

真实

最新更新