在我使用jsonpickle(下面的代码A)之前,我无法序列化Twitter API结果,但后来我无法解码json文件(下面的代码B)。代码A创建了一个大的json对象。请帮忙。
代码A
#!/usr/bin/env python
import tweepy
import simplejson as json
import jsonpickle
consumer_key = "consumer key here"
consumer_secret = "consumer secret here"
access_token = "access token here"
access_token_secret = "access token secret here"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.ModelParser())
jsonFile = open('/location_here/results.json', 'wb')
for status in tweepy.Cursor(api.search, q="keyword").items():
print status.created_at, status.text
frozen = jsonpickle.encode(status)
s = json.loads(frozen)
json.dump(s, jsonFile)
代码B
import jsonpickle
import simplejson as json
in_file = open("/location_here/results.json")
out_file = open("/location_here/data.json", 'wb')
jsonstr = in_file.read()
thawed = jsonpickle.decode(jsonstr)
data = json.loads(thawed)
这会产生错误,ValueError:尾随数据。
感谢。