2016-07-28 8 views
0

jsonに例外エラーを格納しようとしています。私は文字列を格納していると確信していますが、それでも私は型エラーを与えています。コードのJSON TypeError:予想される文字列またはバッファー

関連セクション:

except ConnectionError as e: 
     s = str(e) 
     print type(s) 
     data = json.loads({'error message': s}) 
     print "JSON load succeeded" 

トレースバック:

<type 'str'> 

Traceback (most recent call last): 
    File "[REDACTED]", line 36, in <module> 
    ping(SERVERS) 
    File "[REDACTED]", line 29, in ping 
    data = json.loads({'error message': s}) 
    File "C:\Python27\Lib\json\__init__.py", line 339, in loads 
    return _default_decoder.decode(s) 
    File "C:\Python27\Lib\json\decoder.py", line 364, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
TypeError: expected string or buffer 

これは私にとって非常に不可解です。私はこの問題について何か助けていただければ幸いです。

答えて

1

json.dumps()ではなくjson.loads()をお探しですか?

data = json.dumps({'error message': s}) 

json.dumps(obj):Pythonオブジェクト

にデシリアライズS(JSON文書を含む strインスタンス):JSONに objをシリアル str
json.loads(s)をフォーマットし、これを試します
関連する問題