0
我做一個API請求。我的問題在於迴應。這是UTF-8 JSON,但我收到的消息\t
和\n
,我不能解碼。Python的請求圖書館的「 t n的響應
郵差頭信息
Connection →keep-alive
Content-Encoding →gzip
Content-Length →1199
Content-Type →application/json; charset=UTF-8
Date →Sun, 07 May 2017 16:13:24 GMT
Server →nginx
Vary →Accept-Encoding
X-Main-Language →es
X-Rate-Limit-Limit →500
X-Rate-Limit-Remaining →497
X-Rate-Limit-Reset →172
X-Total-Count →2
響應在python:
'\t{\n "id": 356557,\n "name": {\n "es": "demo-`nwadigital"\n },\n "description": {\n "es": null\n },\n "type": null,\n "email": "[email protected]",\n "logo": null,\n "contact_email": null,\n "country": "AR",\n "facebook": null,\n "twitter": "https:\\/\\/twitter.com\\/",\n "google_plus": null,\n "instagram": "https:\\/\\/instagram.com\\/",\n "pinterest": null,\n "blog": null,\n "business_id": null,\n "business_name": null,\n "business_address": null,\n "address": null,\n "phone": null,\n "customer_accounts": "optional",\n "plan_name": "Professional",\n "domains": [],\n "languages": {\n "es": {\n "currency": "ARS",\n "active": true\n },\n "pt": {\n "currency": "BRL",\n "active": false\n },\n "en": {\n "currency": "USD",\n "active": false\n }\n },\n "original_domain": "demonwadigital.mitiendanube.com",\n "main_currency": "ARS",\n "current_theme": "material",\n "main_language": "es",\n "admin_language": "es",\n "created_at": "2016-11-04T18:34:48+0000",\n "updated_at": "2016-11-04T18:34:50+0000"\n}'`
def getUrl(url):
i_get = 3
while i_get != 0 :
try:
url = Request(url)
url.add_header('Authentication','bearer xxxxxxx')
response = urllib.request.urlopen(url)
except urllib.error.URLError as e:
i_get = i_get - 1
print('Error en el Get!' + str(i_get))
# log Error
else:
i_get = 0
#data = json.loads(response.read().decode("utf8"))
data = response.read().decode("utf8")
return data
getUrl('https://api.tiendanube.com/v1/xxxxx/store')
有誰知道我應該改變或添加?
你嘗試'response.json()'?這會將它轉換爲Python對象。 – jonrsharpe
'json.loads()'是另一種選擇。 – DyZ
我複製/粘貼你的榜樣和'json.loads'工作。你出了什麼問題? – tdelaney