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

时间:2015-02-17 20:25:35

标签: python arrays json twitter tweepy

我正在尝试在应用中编写一些代码来检查用户是否关注了您。我只是希望代码返回一个我可以置于if循环中的True / False值。

代码是:

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

它会返回所有这些JSON,我对此一无所知,但希望' 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}}}

如何使我的代码返回"以下":false部分的真/假?

(或者," follow_by":第一个数组中的false部分 - 每个数组来自用户""视点"关系)

2 个答案:

答案 0 :(得分:0)

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

尝试:

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

您可以通过指定每个分支来处理JSON数组(我确定很多人都知道大声笑,但我从未使用过JSON!)...

答案 1 :(得分:0)

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