2016-12-06 61 views
0

我正在处理请求,并且我被困住了,因为我正在更改我的源代码。事情是,当我请求对象的网站,它返回以下JSON对象:如何解析这个json对象到Python?

[ 
{ 
"directory":"ca\/48", 
"hash":"ca4860af9e3be43b1d23b823af607be4", 
"height":1839, 
"id":3461818, 
"image":"ca4860af9e3be43b1d23b823af607be4.jpg", 
"change":1480968006, 
"owner":"danbooru", 
"parent_id":null, 
"rating":"s", 
"sample":true, 
"sample_height":1398, 
"sample_width":850, 
"score":0, 
"tags":"1girl breasts cape cleavage defense_of_the_ancients dota_2 green_eyes highres lyralei medium_breasts parted_lips pauldrons red_hair smile solo splashbrush thick_thighs thighs toes", 
"width":1118, 
"file_url":"http:\/\/gelbooru.com\/images\/ca\/48\/ca4860af9e3be43b1d23b823af607be4.jpg" 
} 
] 

我想从"http:\/\/gelbooru.com\/images\/ca\/48\/ca4860af9e3be43b1d23b823af607be4.jpg"

"file_url"值更改为

​​ 所以我通过在我的程序构建器上调用它可以完全使用该网站。 我该怎么做? 在此先感谢。

+0

可能有数百个类似的问题。请在发布可能是重复的内容之前进行一些调查。 –

+0

“json对象”在Python中不太重要。你有一个单一的项目,这是一个字典。如果你知道如何修改一个字典并访问一个列表元素,你会发现其余的。 – bereal

+0

'requests'可以将JSON转换为python dictionary/list - 'response.json' - 它可以将'\ /'转换为正确的文本。 – furas

回答

1

requests可以JSON数据自动转换到Python字典/列表

r = request.get(...) 

data = r.json() 

,然后你有机会

print(data[0]['file_url']) 

,你会看到,有没有\/因为它转化为正确的文本。


实施例与用于由requests内部标准json模块。

text = '''[ 
{ 
"directory":"ca\/48", 
"hash":"ca4860af9e3be43b1d23b823af607be4", 
"height":1839, 
"id":3461818, 
"image":"ca4860af9e3be43b1d23b823af607be4.jpg", 
"change":1480968006, 
"owner":"danbooru", 
"parent_id":null, 
"rating":"s", 
"sample":true, 
"sample_height":1398, 
"sample_width":850, 
"score":0, 
"tags":"1girl breasts cape cleavage defense_of_the_ancients dota_2 green_eyes highres lyralei medium_breasts parted_lips pauldrons red_hair smile solo splashbrush thick_thighs thighs toes", 
"width":1118, 
"file_url":"http:\/\/gelbooru.com\/images\/ca\/48\/ca4860af9e3be43b1d23b823af607be4.jpg" 
} 
]''' 

import json 

data = json.loads(text) 

print(data[0]['file_url'])