简体   繁体   English

如何读取嵌套的 json 转储

[英]How to read nested json dumps

hey guys i am trying to read a json dump but don't know how to read the nested json dumps like lastTransactionID, or NAV.嘿伙计们,我正在尝试读取 json 转储,但不知道如何读取嵌套的 json 转储,例如 lastTransactionID 或 NAV。

I am also receiving the Attribute Error 'AccountDetails' object has no attribute 'get.我还收到属性错误“AccountDetails”对象没有属性“get”。

r = accounts.AccountDetails(accountID)
client.request(r)
print r.response
print r.get('lastTransactionID')
print r.get('NAV')
print r.get('unrealizedPL')

 {
    u'account': 
        {
        u'trades': [], 
        u'marginCloseoutNAV': u'99999.9998',
        u'marginUsed': u'0.0000', 
        u'marginCloseoutPositionValue': u'0.0000', 
        u'currency': u'EUR', 
        u'resettablePL': u'-0.0002', 
        u'NAV': u'99999.9998', 
        u'marginCloseoutMarginUsed': u'0.0000', 
        u'openTradeCount': 0, 
        u'marginCallMarginUsed': u'0.0000', 
        u'orders': [], 
        u'openPositionCount': 0, 
        u'positionValue': u'0.0000', 
        u'pl': u'-0.0002', 
        u'financing': u'0.0000', 
        u'pendingOrderCount': 0, 
        u'positions': 
            [
                    {
                    u'financing': u'0.0000',
                    u'short': 
                        {
                        u'units': u'0', 
                        u'financing': u'0', 
                        u'resettablePL': u'0.0000', 
                        u'unrealizedPL': u'0.0000', 
                        u'pl': u'0.0000'
                        }, 

                    u'commission': u'0.0000', 
                    u'unrealizedPL': u'0.0000', 

                    u'long': 
                        {
                        u'units': u'0', 
                        u'financing': u'0.0000', 
                        u'resettablePL': u'-0.0002', 
                        u'unrealizedPL': u'0.0000', 
                        u'pl': u'-0.0002'
                        }, 

                    u'instrument': u'AUD_USD', 
                    u'resettablePL': u'-0.0002', 
                    u'pl': u'-0.0002'
                    }
            ], 

        u'unrealizedPL': u'0.0000', 
        u'alias': u'Primary', 
        u'createdByUserID': xxx, 
        u'marginCloseoutUnrealizedPL': u'0.0000', 
        u'createdTime': u'2017-05-25T18:26:03.961466233Z', 
        u'balance': u'99999.9998'
        }, 

        u'lastTransactionID': u'255'
    }

Does someone know how to read the nested elements from the json dump?有人知道如何从 json 转储中读取嵌套元素吗? Thanks for your help in advance提前感谢您的帮助

You can use json module for this:您可以为此使用 json 模块:

import json

a = json.load(string_or_file_with_json)
# Now in a you have a dict, that represents json object
# You can write a simple recursive function, that will do something on every entry or smt like that
def do(json_dict):
   for k, v in json_dict.items():
       # !UPDATE, use isinstance(v, dict) instead of v is dict
       # Thx mister in comments
       if isinstance(v, dict): # if v is dict, than it's a nested json object
           do(x)
       else: # else it specific value
           your_action(k, v)

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

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