raspberry pi 2 的代码错误

error in code for raspberry pi 2

在我的代码得到一些帮助后,仍然出现一些错误。

import urllib.request
import json
r = urllib.request.urlopen("http://www.countdown.tfl.gov.uk/stopBoard/50051").read()
rr = str(r)

obj = json.loads(rr)

# filter only the b16 objects
b16_objs = filter(lambda a: a['routeName'] == 'B16',  obj['arrivals'])

if b16_objs:
# get the first item
b16 = b16_objs[0]
my_estimatedWait = b16['estimatedWait']
print(my_estimatedWait)

这是我得到的错误,我不确定如何解决这个问题,因为我是 python 和 raspberry pi 的新手 2。谢谢

File "/usr/lib/python3.2/json/decoder.py", line 369, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "program6.py", line 6, in <module>
obj = json.loads(rr)
File "/usr/lib/python3.2/json/__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.2/json/decoder.py", line 353, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.2/json/decoder.py", line 371, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

感谢您的帮助

rr = str(r) 行替换为:

rr = r.decode()

您需要解码字节对象以生成字符串

这将解决您的问题。

>>> import urllib.request
>>> import json
>>> r = urllib.request.urlopen("http://www.countdown.tfl.gov.uk/stopBoard/50051").read()
>>> json.loads(r.decode('utf-8'))
{'arrivals': [{'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': 'due', 'destination': 'Erith', 'routeName': 'B12', 'routeId': 'B12'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': 'due', 'destination': 'Horn Park', 'routeName': 'B15', 'routeId': 'B15'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': '1 min', 'destination': 'Lewisham Centre', 'routeName': '89', 'routeId': '89'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': '1 min', 'destination': 'Woolwich', 'routeName': '96', 'routeId': '96'}, {'isCancelled': False, 'scheduledTime': '08:39', 'isRealTime': True, 'estimatedWait': '2 min', 'destination': 'New Eltham', 'routeName': 'B13', 'routeId': 'B13'}, {'isCancelled': False, 'scheduledTime': '08:45', 'isRealTime': True, 'estimatedWait': '8 min', 'destination': 'Kidbrooke', 'routeName': 'B16', 'routeId': 'B16'}, {'isCancelled': False, 'scheduledTime': '08:48', 'isRealTime': True, 'estimatedWait': '11 min', 'destination': 'North Greenwich', 'routeName': '486', 'routeId': '486'}, {'isCancelled': False, 'scheduledTime': '08:51', 'isRealTime': True, 'estimatedWait': '14 min', 'destination': 'Woolwich', 'routeName': '96', 'routeId': '96'}, {'isCancelled': False, 'scheduledTime': '08:55', 'isRealTime': True, 'estimatedWait': '19 min', 'destination': 'Horn Park', 'routeName': 'B15', 'routeId': 'B15'}, {'isCancelled': False, 'scheduledTime': '08:57', 'isRealTime': True, 'estimatedWait': '20 min', 'destination': 'Lewisham Centre', 'routeName': '89', 'routeId': '89'}, {'isCancelled': False, 'scheduledTime': '08:58', 'isRealTime': True, 'estimatedWait': '22 min', 'destination': 'North Greenwich', 'routeName': '422', 'routeId': '422'}], 'lastUpdated': '09:36', 'filterOut': [], 'serviceDisruptions': {'infoMessages': [], 'criticalMessages': [], 'importantMessages': []}}

我觉得luoluo answer should solve your problem. However I think you need to get a look on requests library since it has built-in json decoder

您的代码使用请求:

import requests
obj  = requests.get("http://www.countdown.tfl.gov.uk/stopBoard/50051").json()

b16_objs = list(filter(lambda a: a['routeName'] == 'B16',  obj['arrivals']))                                                                           

if b16_objs:
    estimated_wait = b16_objs[0]['estimatedWait']
    print(estimated_wait)