迭代json对象/字典

时间:2016-12-20 18:00:17

标签: python json loops dictionary iteration

abc = {
"orders": [
    {
        "orderID": 5,
        "cost": 10,
        "sell": 15
    },
    {
        "orderID": 6,
        "cost": 8,
        "sell": 12
    },
    {
        "orderID": 7,
        "cost": 15,
        "sell": 26           
        }
    ]
}

for key, value in abc.items():
print (value["orderID"])

我正在尝试提取orderID值,但似乎无法使其工作。它应该回应 5,6,7

2 个答案:

答案 0 :(得分:2)

现在你正在循环abc dict。相反,您希望循环遍历abc['orders']

for order in abc['orders']:
    print (order["orderID"])

答案 1 :(得分:0)

for d in abc['orders']:
    print (d["orderID"])

abc有一个键orders,这是一个列表。所以你可以遍历列表。

相关问题