在数组中附加多个值

时间:2019-03-19 14:02:07

标签: python hubspot hubspot-crm

'我正在尝试从Hubspot获取交易数据,在此示例中,我试图获取Dealid和交易名称以简化问题,但稍后我将提供更多属性。我有以下代码,为我提供了一个DealIds数组和一个交易名称数组。我可以做到这一点,而不是多个数组,而是得到以下内容:

{{12345,'deal1'}, {12346,'deal2'}, {12347,'deal3'}}

或类似的东西

{{'dealId': 12345, 'dealname' : 'deal1'}}

到目前为止,这是我的代码:

deals = []
names = []

def getdeals():
    apikey = "demo"
    url = 'https://api.hubapi.com/deals/v1/deal/paged?hapikey='+apikey+'&properties=dealname&limit=250'
    response = requests.get(url)
    jsonDeals = response.json()

    for deal in jsonDeals['deals']:
       properties = deal['properties']
       deals.append(deal['dealId'])
       names.append(properties['dealname']['value'])

3 个答案:

答案 0 :(得分:1)

您已经在json中保存了数据。正是您要映射和存储它的方式。

output={}
def getdeals():
    apikey = "demo"
    url = 'https://api.hubapi.com/deals/v1/deal/paged?hapikey='+apikey+'&properties=dealname&limit=250'
    response = requests.get(url)
    jsonDeals = response.json()

    for deal in jsonDeals['deals']:
       properties = deal['properties']
       output.update({deal['dealId']: properties['dealname']['value']})

答案 1 :(得分:1)

这可以使用列表理解来解决:

[{'dealId':deal['dealId'],'dealname':deal['properties']['dealname']['value']} for deal in jsonDeals['deals']]

答案 2 :(得分:0)

AS E.Serra建议Deal_obj = {'dealname':properties ['dealname'] ['value'],'dealid':deal ['dealId']}解决了该问题。

这是更新的代码:

%%time
deals = []

def getdeals():
    apikey = "demo"
    url = 'https://api.hubapi.com/deals/v1/deal/paged?hapikey='+apikey+'&properties=dealname&limit=250'
    response = requests.get(url)
    jsonDeals = response.json()

    for deal in jsonDeals['deals']:
       properties = deal['properties']
       deal_obj = {'dealname': properties['dealname']['value'], 'dealid':deal['dealId']}
       deals.append(deal_obj)