简体   繁体   English

如何访问彼此嵌套的多个字典和列表层并将它们收集到列表中

[英]how to access several layers of dictionaries and lists nested within each other and collect them into a list

im looking to access the urls of the images in this json file. 我希望访问此json文件中的图像的URL。 However I can only get so far in the nest. 但是我只能在巢中走得这么远。 I got as far as Image which prints out ' "url": //images.... ' 我得到的图像打印出“url”:// images ....

{
    "devices": [
        {
            "variants": 
            [
                {
                    "iMEIPrefix": null,

                    {
                        "totalTax": 0,

                    },
                    "smartTab": {
                        "payOffPeriod": 24,
                        "requiredUpfrontPayment": 0,

                    },
                    "pricingForUi": {
                        "payOffPeriod": 24,
                        "requiredUpfrontPayment": 0,

                    },

                    "memory": "64GB",
                    "phoneImages": [
                        {
                            "url": "//images.ctfassets.net/7bx5buq4osbe/4tfPcjBNmnbWupJ7byONiZ/0f27dc736403c4027bbf13f184ffd4fc/PIXEL3A-BLACK-FRONT.png",
                            "title": "PIXEL3A-BLACK-FRONT",
                            "description": null
                        },
                        {
                            "url": "//images.ctfassets.net/7bx5buq4osbe/1UVKkXIBCUPkKJ8ROvFkOO/8f25b2c57bfd774792dfb69ed1d3cc29/PIXEL3A-BLACK-SIDE.png",
                            "title": "PIXEL3A-BLACK-SIDE",
                            "description": null
                        },
                        {
                            "url": "//images.ctfassets.net/7bx5buq4osbe/1K2AP67ZhWb9pBtIp3r1sj/755bdc263d4a6e43e8275bea2beb92d3/PIXEL3A-BLACK-BACK.png",
                            "title": "PIXEL3A-BLACK-BACK",
                            "description": null
                        }
                    ],

my code thus far 我的代码到目前为止

phoneImages = phone['variants'][0]['phoneImages']

data['image'] = phoneImages

for example im hoping to have this output: 例如我希望有这个输出:

"image": [                                      
    "https://xpressphone-backend.herokuapp.com/Apple iPhone XR/xr-black-front.png",
    "https://xpressphone-backend.herokuapp.com/Apple iPhone XR/xr-black-back.png"
],

It's not a valid json file, you should remove {"totalTax": 0}, this part or convert into "totalTax": 0 它不是一个有效的json文件,你应该删除{"totalTax": 0},这部分或转换为"totalTax": 0

data = {}
phoneImages = [image['url'] for image in phone['devices'][0]['variants'][0]['phoneImages']]
data['image'] = phoneImages
print(data)

O/P: O / P:

{'image': ['//images.ctfassets.net/7bx5buq4osbe/4tfPcjBNmnbWupJ7byONiZ/0f27dc736403c4027bbf13f184ffd4fc/PIXEL3A-BLACK-FRONT.png', '//images.ctfassets.net/7bx5buq4osbe/1UVKkXIBCUPkKJ8ROvFkOO/8f25b2c57bfd774792dfb69ed1d3cc29/PIXEL3A-BLACK-SIDE.png', '//images.ctfassets.net/7bx5buq4osbe/1K2AP67ZhWb9pBtIp3r1sj/755bdc263d4a6e43e8275bea2beb92d3/PIXEL3A-BLACK-BACK.png']}
urls = []

phoneImages = phone['variants'][0]['phoneImages']

for phoneImage in phoneImages:
  urls.append = phoneImage['url']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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