2016-05-25 10 views
0

私は理解できない問題があります。私は(キューとして)Redisのを経由して文字列としてJSONデータを送信し、受信機は、次のエラー投げている:Jsonエラー:「end is bounds」?

try: 
    item = json.loads(item[1]) 
except ValueError as e: 
    sys.stderr.write("[ERROR JSON (in queue)] - {1} => {0}\n".format(str(e), str(item))) 
    return None 

何が本当にある:

[ERROR JSON (in queue)] - {"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature": 
"6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"} => end is out of bounds 

例外をスローするコードは次のようであるが

>>> import json 
>>> s = '{"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature": "6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"}' 
>>> print s 

私は何の問題、文字列(コピー/ Pythonのコンソールに貼り付け)が全くエラーを得なかったが、私の元のコードは、throのである:奇数は、私はPythonのコンソールを開き、次の操作を実行した場合ということです翼1!

問題の原因は何ですか?

答えて

4

あなたは、文字列itemsの2番目の文字であるitem[1]を、ロードされています

>>> json.loads('"') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads 
    return _default_decoder.decode(s) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode 
    obj, end = self.scan_once(s, idx) 
ValueError: end is out of bounds 

あなたが書く必要があります:

item = json.loads(item) 
+0

しかし、何が本当に奇妙なことは、この問題は、ほんの数回発生していることです。 'item'は、0のRedisのキーと1の値(jsonデータ)を含むタプルです。時には、jsonの値が完全な項目に入っているのですが、なぜか分かりません。 –

+0

これはあなたの質問の一部ではありません。新しい質問をする必要があります。なぜRedisはタプルではなく文字列を返すのですか? – Daniel

+0

はい、あなたは正しいです。あなたの答えはここの私の質問のための正しいものです。 –