简体   繁体   中英

Iterating over Instagram json

I received a JSON object as a response to an Instagram API call.

I want to access the URLs of all pictures in the response, so far I managed to access the first image only:

from pprint import pprint
with open('test.json') as data_file:
    mydata = json.load(data_file)
    print mydata["data"][0]["images"]["standard_resolution"]["url"]

I'm struggling how to iterate over mydata correctly.

The json looks roughly like:

{
"meta:":{}
"data":[
    {}
    {}
    {}
}

I get stuck at this nested loop:

for x in mydata["data"]:
   for y in x:
       print y

outputs

attribution
tags
user
comments
filter
images
link
location
created_time
users_in_photo
caption
type
id
likes 
dataset = mydata['data']
for data in dataset:
   url = data['images']['standard_resolution']['url']

If your first image is accessed as -

mydata["data"][0]["images"]["standard_resolution"]["url"]

Then you should iterate over mydata["data"] , which is a list, using for loop and get each url from each dictionary in it.

Example -

with open('test.json') as data_file:
    mydata = json.load(data_file)
    for img in mydata["data"]:
        print img["images"]["standard_resolution"]["url"]

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