简体   繁体   中英

How do we get dict value from object with changing variable

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

print(x)

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.

How do we assign order number to variable?

You could iterate over the dictionary with .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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