0
我在Python 3.2中遇到了以下代碼的問題,可能是由於某些編碼錯誤。 json.loads函數給出以下錯誤消息:「沒有JSON對象可以被解碼」。奇怪的是,當我打印出data_object.decode(編碼)時,它提供了一個有效的JSON對象。下面的代碼:使用urllib.request和json模塊(#2)在Python中加載JSON對象
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中執行相同的字符串操作,它會一直給我提供相同的錯誤。請幫幫我!