简体   繁体   中英

Parsing JSON with Python TypeError: list indices must be integers or slices, not str

i read about 10 to 12 answer but nothing help me.

i wanna print the url shows in picute click to see this is my code :

> from urllib.request import urlopen 
> import json as simplejson

> link = "https://api.instagram.com/v1/users/self/media/recent/?access_token=2290710571.098b867.5cf99a24896b476982c70c47eb0a0413"

> response = urlopen(link)
> data = simplejson.load(response) 
> print (data['data']['0']['image']['standard_resolution']['url'])

but i get this error :

TypeError: list indices must be integers or slices, not str

edit1:

i saw someone say put [0] in you code. if i change

print (data['data']['0']['image']['standard_resolution']['url'])

to this

print (data[0]['data']['0']['image']['standard_resolution']['url']) i will get new error -> KeyError: 0

edit2:

and if i change

data = simplejson.load(response)

to this

pdata = simplejson.loads(response)

i will get new error ->

aise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not HTTPResponse

The advice is to use an integer, not to add an extra two lookups.

print(data['data'][0]['images']['standard_resolution']['url'])

Note, that data has images not image .

(Also, there's no reason to import the json library as simplejson. simplejson was a library that was popular before json was included in the standard library; but that was added in version 2.6 and we're on 3.7 now. Just import json and use it.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM