Python 如何迭代JSON数组并获取本身是JSON对象的键的值
我一直在尝试做一些简单的事情,但我很难解决它 我有一个json对象,看起来像:Python 如何迭代JSON数组并获取本身是JSON对象的键的值,python,json,iterator,Python,Json,Iterator,我一直在尝试做一些简单的事情,但我很难解决它 我有一个json对象,看起来像: jsonObject = { 'attributes': { '192': { <--- This can be changed times to times meaning different number 'id': '192', 'code': 'hello', 'label': 'world',
jsonObject = {
'attributes': {
'192': { <--- This can be changed times to times meaning different number
'id': '192',
'code': 'hello',
'label': 'world',
'options': [
{
'id': '211',
'label': '5'
},
{
'id': '1202',
'label': '8.5'
},
{
'id': '54',
'label': '9'
},
{
'id': '1203',
'label': '9.5'
},
{
'id': '58',
'label': '10'
}
]
}
},
'template': '12345',
'basePrice': '51233',
'oldPrice': '51212',
'productId': 'hello',
}
如何获取标签和id?您可以尝试以下代码:
attr=jsonObject['attributes']
temp=list(attr.values())[0]#它与“temp=attr['192']”相同,但您说过“192”可以更改。
options=temp['options']
对于选项中的选项:
打印(f“id:{option['id']},label:{option['label']}”)
我爱你!这正是我要找的!我想您也可以将列表中的选项(attributes['attributes'].values())[0]['options']:请欣赏!这正是我要找的@哦,我在你的评论中发现了一个错误。它应该是列表中选项的(jsonObject['attributes'].values())[0]['options']:
@gragratornewbie No problem:)编程愉快!
for att, value in jsonObject.items():
print(f"{att} - {value}"