我遇到了Python和JSON問題。通過Redis的Python和JSON似乎不起作用
1)我通過JSON發送字典給Redis。我使用JSON傾倒在生產者和負載從消費者的字典:
#This is a dictionary
args = {"last_observed_date": "2014-04-08T02:05:00", "tau": 2, "interval": 5, "backcast": 5, "series": "exr:EUR_USD:2014-04-08T02:05:00", "K": 5, "is_difference": false, "M": 3, "is_log": false, "last_time": null, "pair": "EUR_USD", "granularity": "minute", "series_name": "closeBid", "method": [{"ols": {"alpha": 0.5}}]}
製作公司服務器:
args = json.dumps(args)
r.lpush(model_queue,args)
消費者服務器:
args = r.brpop(model_queue,0)[1]
args = json.loads(args)
Traceback (most recent call last):
File "/home/ubuntu/workspace/forex-trading/chaos/chaos_worker.py", line 38, in <module>
data = json.loads(data)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
我的問題是爲什麼,當我傾倒作爲字符串的字典,我從Redis檢索時無法解碼?
流是這樣的:dictionary to string - > redis - > string to dictionary。我傾倒在生產者身上並加載到消費者身上。
你忘了提問了。 – Adam
你傳遞給'json.loads'的是什麼類型? – mgilson
它是一本字典。哈哈 – Tampa