使用urllib.request和json模块在Python中加载JSON对象(#2)

时间:2017-05-20 18:17:00

标签: python json

我在Python 3.2中遇到以下代码时遇到麻烦,可能是由于某些编码错误。 json.loads-function提供以下错误消息:“无法解码JSON对象”。奇怪的是,当我打印出data_object.decode(encoding)时,它会给出一个有效的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函数中并在之前和之后添加'-characters,它可以完美地工作。如果我尝试在Python中执行相同的字符串操作,它会一直给我同样的错误。请帮帮我!

1 个答案:

答案 0 :(得分:0)

问题解决了,升级到Python 3.4,它就像一个魅力