【问题标题】:how can i get the python 'json' list, dictionary Importing mixed data我怎样才能得到 python 'json' 列表,字典导入混合数据
【发布时间】:2022-06-27 17:35:31
【问题描述】:

我要打印,python 'json' 列表,字典导入混合数据

考试json数据

{
    "data": [
        {
            "rows": [
                {
                    "dimensions": [
                        {
                            "value": "title1"
                        }
                    ],
                    "metrics": [
                        {
                            "value": "potato"
                        },
                        {
                            "value": "sweetpotato"
                        },
                        {
                            "conversion_annotation_value": []
                        },
                        {
                            "value": "123123"
                        }
                    ]
                },
            ]
        }
    ],
} 

据我所知,我使用 try

itemlist = json.loads(r.text)
for i in itemlist['data']['rows']['dimensions']:
...
for i in itemlist['data']['rows']['metrics']:
...

错误,我看到了

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

我想看看,这个json数据

'title1'
'123123'

【问题讨论】:

    标签: python json list dictionary


    【解决方案1】:

    使用itemlist['data']['rows'].get('dimensions', []) 而不是使用字典的.get() 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-22
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2022-11-26
      相关资源
      最近更新 更多