django - 在json响应对象之间进行迭代

时间:2017-10-01 04:23:40

标签: python json django

我有一个响应对象,我从api调用中收到。响应有几个在一次调用中返回的对象。我想要做的是从返回的每个对象中获取信息并将它们存储在varialbes中以在应用程序中使用它们。我知道在返回单个对象时从json响应中获取信息但是我对多个对象感到困惑...我知道如何通过类似forloop的东西自动化迭代过程...它不会迭代。

这是我得到的示例回复:

我想从两个项目中抓取_id。

{  
   'user':"<class 'synapse_pay_rest.models.users.user.User'>(id=..622d)",
   'json':{  
      '_id':'..6e80',
      '_links':{  
         'self':{  
            'href':'https://uat-api.synapsefi.com/v3.1/users/..22d/nodes/..56e80'
         }
      },
      'allowed':'CREDIT-AND-DEBIT',
      'client':{  
         'id':'..26a34',
         'name':'Charlie Brown LLC'
      },
      'extra':{  
         'note':None,
         'other':{  

         },
         'supp_id':''
      },
      'info':{  
         'account_num':'8902',
         'address':'PO BOX 85139, RICHMOND, VA, US',
         'balance':{  
            'amount':'750.00',
            'currency':'USD'
         },
         'bank_long_name':'CAPITAL ONE N.A.',
         'bank_name':'CAPITAL ONE N.A.',
         'class':'SAVINGS',
         'match_info':{  
            'email_match':'not_found',
            'name_match':'not_found',
            'phonenumber_match':'not_found'
         },
         'name_on_account':' ',
         'nickname':'SynapsePay Test Savings Account - 8902',
         'routing_num':'6110',
         'type':'BUSINESS'
      },
<class 'synapse_pay_rest.models.nodes.ach_us_node.AchUsNode'>({  
   'user':"<class 'synapse_pay_rest.models.users.user.User'>(id=..622d)",
   'json':{  
      '_id':'..56e83',
      '_links':{  
         'self':{  
            'href':'https://uat-api.synapsefi.com/v3.1/users/..d622d/nodes/..6e83'
         }
      },
      'allowed':'CREDIT-AND-DEBIT',
      'client':{  
         'id':'599378ec6aef1b0021026a34',
         'name':'Charlie Brown LLC'
      },
      'extra':{  
         'note':None,
         'other':{  

         },
         'supp_id':''
      },
      'info':{  
         'account_num':'8901',
         'address':'PO BOX 85139, RICHMOND, VA, US',
         'balance':{  
            'amount':'800.00',
            'currency':'USD'
         },
         'bank_long_name':'CAPITAL ONE N.A.',
         'bank_name':'CAPITAL ONE N.A.',
         'class':'CHECKING',
         'match_info':{  
            'email_match':'not_found',
            'name_match':'not_found',
            'phonenumber_match':'not_found'
         },
         'name_on_account':' ',
         'nickname':'SynapsePay Test Checking Account - 8901',
         'routing_num':'6110',
         'type':'BUSINESS'
      },
})

这是我的代码: 它不会抓住任何价值......

需要对节点变量进行迭代,该变量是hte json响应对象。

def listedLinkAccounts(request):
    currentUser = loggedInUser(request)
    currentProfile = Profile.objects.get(user = currentUser)

    user_id = currentProfile.synapse_id
    synapseUser = SynapseUser.by_id(client, str(user_id))

    options = {
        'page':1,
        'per_page':20,
        'type': 'ACH-US',
    }

    nodes = Node.all(synapseUser, **options)
    print(nodes)

    response = nodes
    _id = response["_id"]
    print(_id)

    return nodes

这是来自api文档的api响应示例:

{
    "error_code": "0",
    "http_code": "200",
    "limit": 20,
    "node_count": 5,
    "nodes": [
        {
            "_id": "594e5c694d1d62002f17e3dc",
            "_links": {
                "self": {
                    "href": "https://uat-api.synapsefi.com/v3.1/users/594e0fa2838454002ea317a0/nodes/594e5c694d1d62002f17e3dc"
                }
            },
            "allowed": "CREDIT-AND-DEBIT",
            "client": {
                "id": "589acd9ecb3cd400fa75ac06",
                "name": "SynapseFI"
            },
            "extra": {
                "other": {},
                "supp_id": "ABC124"
            },
            "info": {
                "account_num": "7443",
                "address": "PLACE DE LA REPUBLIQUE 4 CROIX 59170 FR",
                "balance": {
                    "amount": "0.00",
                    "currency": "USD"
                },
                "bank_long_name": "3 SUISSES INTERNATIONAL",
                "bank_name": "3 SUISSES INTERNATIONAL",
                "name_on_account": " ",
                "nickname": "Some Account"
            },
            "is_active": true,
            "timeline": [
                {
                    "date": 1498307689471,
                    "note": "Node created."
                },
                {
                    "date": 1498307690130,
                    "note": "Unable to send micro deposits as node type is not ACH-US."
                }
            ],
            "type": "WIRE-INT",
            "user_id": "594e0fa2838454002ea317a0"
        },
        {
          ...
        },
        {
          ...
        },
        ...
    ],
    "page": 1,
    "page_count": 1,
    "success": true
}

0 个答案:

没有答案