Python 斯特里姆利斯腾纳有';t在tweepy中返回JSON格式的数据

Python 斯特里姆利斯腾纳有';t在tweepy中返回JSON格式的数据,python,json,tweepy,Python,Json,Tweepy,我的目标是以json格式从状态(self,status)上的def获取数据。 现在我以类似于JSON的方式返回数据,但它不是JSON: {'favorited': False, 'contributors': None, 'truncated': False, 'text': 'Lol. As long as you and Tsidi are awake, am not sleeping RT @TboyMP: @Fufu_Tinkies hambo lala wena!! LMAO!!',

我的目标是以json格式从状态(self,status)上的
def获取数据。
现在我以类似于JSON的方式返回数据,但它不是JSON:

{'favorited': False, 'contributors': None, 'truncated': False, 'text': 'Lol. As long as you and Tsidi are awake, am not sleeping RT @TboyMP: @Fufu_Tinkies hambo lala wena!! LMAO!!', 'source_url': 'http://ubersocial.com', 'in_reply_to_status_id': None, 'user': <tweepy.models.User object at 0x1b4f3d0>, 'filter_level': 'medium', 'geo': None, 'id': 326808604013379586, 'favorite_count': 0, 'source': 'UberSocial for BlackBerry', 'lang': 'en', 'author': <tweepy.models.User object at 0x1b4f3d0>, 'created_at': datetime.datetime(2013, 4, 23, 21, 23, 37), 'retweeted': False, 'coordinates': None, 'in_reply_to_user_id_str': None, 'entities': {'symbols': [], 'user_mentions': [{'id': 282499717, 'indices': [60, 67], 'id_str': '282499717', 'screen_name': 'TboyMP', 'name': 'Thulane Khanye'}, {'id': 157961325, 'indices': [69, 82], 'id_str': '157961325', 'screen_name': 'Fufu_Tinkies', 'name': 'Nomfundo'}], 'hashtags': [], 'urls': []}, 'in_reply_to_status_id_str': None, 'in_reply_to_screen_name': None, 'id_str': '326808604013379586', 'place': None, 'retweet_count': 0, 'in_reply_to_user_id': None}
但是,我不能用json模块
加载它:它给了我一个错误:

Encountered Exception: expected string or buffer
那么,如何从JSON中删除数据或将其转换为JSON呢

更新 我想要这样的东西:

>>> data = '[{"fooo":"bar","something":"another bar"}]'
>>> ww = json.loads(data)
>>> ww[0]['fooo']
u'bar'
我希望通过
tweepy
数据来获得帮助。。。。
谢谢。

您有一个Python字典,要将其转换为JSON,请使用:


这将把
def on_status(self,status):
中的原始json数据写入文件

此解决方案取自,用于将原始json数据添加到status对象,并作为使用tweepy流功能的框架代码

@classmethod                    
def parse(cls, api, raw):
        status = cls.first_parse(api, raw)
        setattr(status, 'json', json.dumps(raw))
        return status

tweepy.models.Status.first_parse = tweepy.models.Status.parse
tweepy.models.Status.parse = parse

_dir = os.path.dirname(os.path.abspath(__file__))

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print status.user.screen_name
        with codecs.open(os.path.join(_dir, 'tweets.json'), "a", 'utf-8') as textFile:
                textFile.write(status.json)
                textFile.write('\n')

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

我不是Python专家,但现在的情况似乎是,正在使用新版本对解析方法进行热修补,然后将新版本安装到tweepy类层次结构中。新版本调用旧版本,然后将一个json插槽附加到从原始解析方法返回的对象。

您在问题中打印的结构是一个Python字典,它与
json.loads()
将返回的内容相同。你应该能够以你想要的方式使用你已经拥有的任何东西,例如
data['favorited']
,而不需要担心任何JSON转换。如果我做类似的事情:
data=我粘贴的东西
,我会得到
语法错误:无效语法
,除了它似乎不是递归的,因此,它不会钻取“User”键的用户对象并获取其所有属性。相反,我得到了这个错误:
TypeError:is not JSON serializable
@ClaytonStanley听起来你的问题有点不同,我建议问一个新问题。
import json
json_data = json.dumps(data)
@classmethod                    
def parse(cls, api, raw):
        status = cls.first_parse(api, raw)
        setattr(status, 'json', json.dumps(raw))
        return status

tweepy.models.Status.first_parse = tweepy.models.Status.parse
tweepy.models.Status.parse = parse

_dir = os.path.dirname(os.path.abspath(__file__))

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print status.user.screen_name
        with codecs.open(os.path.join(_dir, 'tweets.json'), "a", 'utf-8') as textFile:
                textFile.write(status.json)
                textFile.write('\n')

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream