简体   繁体   English

我们如何通过更改变量从 object 获取 dict 值

[英]How do we get dict value from object with changing variable

x = json.loads(y) #y is received from sender

print(x)

Output: Output:

[
    [
        {
            "O2JUKZ-4QUHH-6ZUZWF": {
                "avg_price": "0.00000",
                "cost": "0.00000",
                "descr": {
                    "close": None,
                    "leverage": "0"
                },
                "status": "open",
                "stoprice": "0.00000"
            }
        }
    ],
    "openOrders",
    {
        "sequence": 1
    }
]

Question : How do we assign order number (the characters in bold) to a variable, when It always changes?问题:当它总是改变时,我们如何将订单号(粗体字符)分配给变量?

Already tried: x[0][0][' O2JUKZ-4QUHH-6ZUZWF ']['status'] to get the status (which works), but doesn't work anymore when " O2JUKZ-4QUHH-6ZUZWF " changes.已经尝试: x[0][0][' O2JUKZ-4QUHH-6ZUZWF ']['status'] 获取状态(有效),但当“ O2JUKZ-4QUHH-6ZUZWF ”更改时不再有效。

How do we assign order number to variable?我们如何将订单号分配给变量?

You could iterate over the dictionary with .items()您可以使用.items()遍历字典

my_dictionary = data[0][0]
for order_code, sub_dict in my_dictionary.items():
    # order_code is the dictionary key and order code you want
    # sub_dict.get("status") will be the status

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

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