【问题标题】:Extract single True/False value from JSON Twitter response ( Tweepy and Python )从 JSON Twitter 响应中提取单个 True/False 值(Tweepy 和 Python)
【发布时间】:2015-04-18 16:17:56
【问题描述】:

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

代码是:

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

它返回所有这些 JSON,我对此一无所知,但想要“following”下其他用户的第二个数组中的“false”值(或者,“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}}}

如何让我的代码为“以下”:false 部分返回真/假?

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

【问题讨论】:

    标签: python arrays json twitter tweepy


    【解决方案1】:

    很好,发布后立即解决。

    试试:

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

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

    【讨论】:

      【解决方案2】:
      print api.show_friendship(target_screen_name=screen_name)[1].following
      

      是的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-02
        • 2019-04-17
        • 1970-01-01
        • 2014-01-25
        • 1970-01-01
        • 2017-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多