2017-05-20 13 views
0

おそらくコード化エラーのために、Python 3.2で次のコードに問題があります。 json.loads-functionは、「JSONオブジェクトがデコードできませんでした」というエラーメッセージを表示します。奇妙なことは、data_object.decode(エンコーディング)を出力すると、有効なJSONオブジェクトが得られるということです。ここでurllib.requestとjsonモジュールを使用してPythonでJSONオブジェクトをロードする(#2)

req = urllib.request.Request(post_request_url,object_json,request_headers) 
file_response_packet = urllib.request.urlopen(req, timeout=2) 
data_object = file_response_packet.read() 
encoding = file_response_packet.info().get_content_charset('utf-8') 
print(encoding) 

try: 
    print(data_object.decode(encoding)) 
    test= json.loads(data_object.decode(encoding)) 
except Exception as e: 
    print(str(e)) 

プリントアウトです::ここでは、コードです

utf-8 
{"code":100,"status":"success","message":"API OK!","restart":"1","save_response":"1","offline_operate":"Yes","result":{"timestamp":"2017-05-20T18:55:22.216Z","channels":{"settings":{"1":{"hours":{"00:00":"false","01:00":"false","02:00":"false","03:00":"false","04:00":"false","05:00":"false","06:00":"false","07:00":"false","08:00":"false","09:00":"false","10:00":"false","11:00":"false","12:00":"false","13:00":"false","14:00":"false","15:00":"false","16:00":"false","17:00":"false","18:00":"false","19:00":"false","20:00":"false","21:00":"false","22:00":"false","23:00":"false"},"override":{"condition":"Less Than","instruction":"true","value":"23","parameter":"in_temp"},"duration":"3600","label":"Temp","hash":"1"},"2":{"hours":{"00:00":"false","01:00":"false","02:00":"false","03:00":"false","04:00":"false","05:00":"false","06:00":"false","07:00":"false","08:00":"false","09:00":"false","10:00":"false","11:00":"false","12:00":"true","13:00":"true","14:00":"true","15:00":"false","16:00":"true","17:00":"false","18:00":"false","19:00":"false","20:00":"false","21:00":"false","22:00":"false","23:00":"false"},"override":{"condition":"Greater Than","instruction":"true","value":"22","parameter":"in_temp"},"duration":"3600","label":"Fan","hash":"2"},"3":{"hours":{"00:00":"false","01:00":"false","02:00":"false","03:00":"false","04:00":"false","05:00":"false","06:00":"false","07:00":"true","08:00":"false","09:00":"false","10:00":"true","11:00":"true","12:00":"true","13:00":"false","14:00":"false","15:00":"false","16:00":"true","17:00":"true","18:00":"false","19:00":"true","20:00":"false","21:00":"false","22:00":"false","23:00":"false"},"override":{"condition":"Less Than","instruction":"false","value":"28","parameter":"out_humidity"},"duration":"3600","label":"Water","hash":"3"},"4":{"hours":{"00:00":"true","01:00":"true","02:00":"true","03:00":"true","04:00":"true","05:00":"true","06:00":"true","07:00":"true","08:00":"true","09:00":"true","10:00":"true","11:00":"true","12:00":"true","13:00":"true","14:00":"true","15:00":"true","16:00":"true","17:00":"true","18:00":"true","19:00":"true","20:00":"false","21:00":"true","22:00":"true","23:00":"true"},"override":{"condition":"Greater Than","instruction":"true","value":"0","parameter":"none"},"duration":"3600","label":"Lights","hash":"4"},"5":{"hours":{"00:00":"false","01:00":"false","02:00":"false","03:00":"false","04:00":"false","05:00":"false","06:00":"false","07:00":"false","08:00":"false","09:00":"true","10:00":"false","11:00":"false","12:00":"false","13:00":"false","14:00":"false","15:00":"false","16:00":"false","17:00":"false","18:00":"false","19:00":"true","20:00":"false","21:00":"false","22:00":"false","23:00":"true"},"override":{"condition":"Greater Than","instruction":"false","value":"20","parameter":"none"},"duration":"0","label":"Temp","hash":"5"},"6":{"hours":{"00:00":"false","01:00":"false","02:00":"false","03:00":"false","04:00":"false","05:00":"false","06:00":"false","07:00":"false","08:00":"false","09:00":"false","10:00":"false","11:00":"false","12:00":"false","13:00":"false","14:00":"false","15:00":"false","16:00":"false","17:00":"false","18:00":"false","19:00":"false","20:00":"false","21:00":"false","22:00":"false","23:00":"false"},"override":{"condition":"Less Than","instruction":"false","value":"0","parameter":"none"},"duration":"0","label":"Misc","hash":"6"},"7":{"hours":{"00:00":"false","01:00":"false","02:00":"false","03:00":"false","04:00":"false","05:00":"false","06:00":"false","07:00":"false","08:00":"false","09:00":"false","10:00":"false","11:00":"false","12:00":"false","13:00":"false","14:00":"false","15:00":"false","16:00":"false","17:00":"false","18:00":"false","19:00":"false","20:00":"false","21:00":"false","22:00":"false","23:00":"false"},"override":{"condition":"Less Than","instruction":"false","value":"0","parameter":"none"},"duration":"0","label":"Temp","hash":"7"},"8":{"hours":{"00:00":"false","01:00":"false","02:00":"false","03:00":"false","04:00":"false","05:00":"false","06:00":"false","07:00":"false","08:00":"false","09:00":"false","10:00":"false","11:00":"false","12:00":"false","13:00":"false","14:00":"false","15:00":"false","16:00":"false","17:00":"false","18:00":"false","19:00":"false","20:00":"false","21:00":"false","22:00":"false","23:00":"false"},"override":{"condition":"Greater Than","instruction":"true","value":"47","parameter":"none"},"duration":"3600","label":"Lights","hash":"8"}},"switches":{"1":"false","2":"true","3":"false","4":"true","5":"false","6":"false","7":"false","8":"false"}}}} 
No JSON object could be decoded 

最後の興味深い発言は私はコンソールからJSONをコピーする場合、「json.loads機能に貼り付けして追加することです前と後の文字は完全に動作します。私がPython内で同じ文字列操作をしようとすると、それは私に同じエラーを与え続けます。私を助けてください!

答えて

0

問題を解決し、Python 3.4にアップグレードし、それが魅力的に機能します

関連する問題