在python中存储这个JSON的正确方法是什么?
我试图存储JSON对象从网站https://www.xkcd.com/info.0.json在python中存储这个JSON的正确方法是什么?
我已经试过
url = 'https://www.xkcd.com/info.0.json'
response = requests.get(url)
if response.status_code == 200:
response_content = str(response.json())
print(response_content)
new_response = response_content.replace("'", '"')
json_data = json.loads(new_response)
print(new_response)
print(json_data)
print(response_content)
回报
{
'link': '',
'month': '11',
'num': 1603,
'title': 'Flashlights',
'safe_title': 'Flashlights',
'year': '2015',
'day': '13',
'img': 'http: //imgs.xkcd.com/comics/flashlights.png',
'transcript': '',
'news': '',
'alt': "Due to a typo, I initially found a forum for serious Fleshlight enthusiasts, and it turns out their highest-end models are ALSO capable of setting trees on fire. They're impossible to use without severe burns, but some of them swear it's worth it."
}
要将单引号转换成response_content
返回,我尝试过
new_response = response_content.replace("'", '"')
但问题与线出现在那里alt
是
.....
"news": "",
"alt": "Due to a typo, ...... of setting trees on fire. They"reimpossibletousewithoutsevereburns,
butsomeofthemswearit"s worth it.",
}
如果里面有任何值的单引号,这种方法失败。
错误日志:
File "./main.py", line 55, in download_latest
json_data = json.loads(new_response)
File "/usr/lib/python3.4/json/__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py", line 359, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 342 (char 341)
加载的JSON在脚本中任何其他的方法呢?
编辑:
我要像做
json_data = json.dumps(response_content)
print(type(json_data)) ## returns <class 'str'>
print(json_data['num'])
但这返回TypeError
File "./main.py", line 53, in download_latest
print(json_data['num'])
TypeError: string indices must be integers
response.json()
方法返回Python数据结构。你在这里做的很多,你只需要:
url = 'https://www.xkcd.com/info.0.json'
response = requests.get(url)
if response.status_code == 200:
json_data = response.json()
就是这样。
您正在将Python数据结构转换为字符串,然后尝试再次将该字符串解释为JSON。这可能看起来像它的工作,因为Python容器的str()
转换使用Python语法来产生结果。但是Python不是JSON,不管怎样,你试图把它变成JSON也是不太好的。并且根本不需要。
您可以直接使用json_data
,它是一个Python字典:
>>> import requests
>>> url = 'https://www.xkcd.com/info.0.json'
>>> response = requests.get(url)
>>> response.status_code
200
>>> json_data = response.json()
>>> type(json_data)
<type 'dict'>
>>> json_data
{u'img': u'http://imgs.xkcd.com/comics/flashlights.png', u'title': u'Flashlights', u'month': u'11', u'num': 1603, u'link': u'', u'year': u'2015', u'news': u'', u'safe_title': u'Flashlights', u'transcript': u'', u'alt': u"Due to a typo, I initially found a forum for serious Fleshlight enthusiasts, and it turns out their highest-end models are ALSO capable of setting trees on fire. They're impossible to use without severe burns, but some of them swear it's worth it.", u'day': u'13'}
>>> print json_data['title']
Flashlights
>>> print json_data['alt']
Due to a typo, I initially found a forum for serious Fleshlight enthusiasts, and it turns out their highest-end models are ALSO capable of setting trees on fire. They're impossible to use without severe burns, but some of them swear it's worth it.
我不知道'response.json()'返回了一个'dict'对象。很好的解释@Martijin :) –
@prodicus:它返回解码的JSON结构。大多数API使用JSON对象,所以结果通常是字典,但是JSON数组也可以(给你一个列表),或任何JSON基元(字符串,整数,浮点数,布尔值,空值)。 –
@prodicus:在这种情况下,它是一个字典,因为这是JSON响应中的内容。 –
试试这个:
import json, requests
r = requests.get('https://www.xkcd.com/info.0.json')
responseJSON = None
if r.status_code == 200:
responseJSON = json.loads(r.content)
print responseJSON # you can access values like responseJSON['img']
因为在这里,你肯定有JSON
响应的,你不妨做
responseJSON = r.json()
注意:您仍必须做错误处理。
'>>> responseJSON == r.json()': 'True' –
@KevinGuan更好!我会补充说,替代方法 – activatedgeek
response.json()
已经返回一个Python字典:
import requests
import json
url = 'https://www.xkcd.com/info.0.json'
response = requests.get(url)
if response.status_code == 200:
response_content = response.json()
print response_content
无需转换和从字符串。
看,'请求'的魔力! –
为什么你想*转换单引号中'response_content' *?为什么不把它像'import json; json.dumps(response_content)'? –
你想通过将'response.json()'串化来改变引号,然后重新解析为JSON来完成什么? – jwodder
我试过'json_data = json.dumps(response_content);打印(键入(json_data))',它说这是''。我想做一些像'xkcd_num = json_data ['num']'但是这会返回一个错误,比如'TypeError:string indices must be integers' –