通过Redis的Python和JSON似乎不起作用

时间:2014-04-08 02:45:20

标签: python json

我在使用Python和JSON时遇到了麻烦。

1)我通过JSON向Redis发送字典。我使用JSON将dict转储到生产者并从使用者加载:

#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检索时无法解码?

Flow就是这样:字典到字符串 - &gt; redis - &gt;字符串到字典。我转储到生产者并加载消费者。

1 个答案:

答案 0 :(得分:0)

你不能将字典传递给json.loads,你必须传递一个json字符串。尝试:

args = json.loads(json.dumps(args))