迭代JSON并追加到字典?

时间:2020-02-03 21:36:11

标签: python dictionary for-loop

我已经搜索过,但似乎无法针对我在这里尝试做的事情给出具体的工作示例。 我正在使用一个API,该API返回一串json。我想遍历所有键/值对,并仅将相关的对存储到另一本字典中,以便以后使用/引用。

大多数情况下,我尝试过的方法都可以工作,但是当我打印字典对象的内容时,它只会返回最新的键/值对...我想知道如何追加到字典中,因此当我打印出来,得到了很长的键/值对列表。

这是代码:

r=requests.get(url + id, headers=h, params=p)
inbound_dict={}
inbound=json.loads(r.text)
for item in inbound['messages']:
    inbound_dict[item['conversationId']] = item['body']
print(inbound_dict)

我知道我必须缺少一些简单的东西。我已经尝试过inbound_dict.append(),但显然这不太正确。

您可以在下面的图像中看到红线的位置,该红线仅显示每个不同的json“实例”的结尾。对于上下文,这是一个短信应用程序,每个json响应代表一条消息。

Here is a sample output of the returned json that I need to parse through

而且,这是文本形式的前2个“ json”:

b'{"id":1005672,"messages":[{"id":4461048,"body":"Mnow test test","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"INCOMING","outgoing":false,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783232355,"attachments":[]},{"id":4461049,"body":"THIS NUMBER DOES NOT CURRENTLY ACCEPT TEXT MESSAGES PLEASE CALL (716) 444-4444 TO WORK WITH ONE OF OUR INTAKE SPECIALISTS","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":0,"status":"RECEIVED","error":null,"kind":"AUTO_RESPONSE","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1576783233546,"attachments":[]},{"id":4620511,"body":"test sms,test sms","conversationId":1005672,"locationId":2045,"contactId":12792806,"assignedUserId":17297,"status":"DELIVERED","error":null,"kind":"API","outgoing":true,"reviewRequest":false,"type":"SMS","readDate":0,"respondedDate":0,"sentDate":1577987093930,"attachments":[]},...

2 个答案:

答案 0 :(得分:1)

我想你想做:

r=requests.get(url + id, headers=h, params=p)
inbound_dict = {}
inbound=json.loads(r.text)
for item in inbound['messages']:
    inbound_dict[item['id']] = item['body']
print(inbound_dict)

提供此数据:

'{"id": 1005672, "messages": [{"id": 4461048, "body": "Mnow test test", "conversationId": 1005672, "locationId": 2045, "contactId": 12792806, "assignedUserId": 0, "status": "RECEIVED", "error": "", "kind": "INCOMING", "outgoing": false, "reviewRequest": false, "type": "SMS", "readDate": 0, "respondedDate": 0, "sentDate": 1576783232355, "attachments": []}, {"id": 4461049, "body": "THIS NUMBER DOES NOT CURRENTLY ACCEPT TEXT MESSAGES PLEASE CALL (716) 444-4444 TO WORK WITH ONE OF OUR INTAKE SPECIALISTS", "conversationId": 1005672, "locationId": 2045, "contactId": 12792806, "assignedUserId": 0, "status": "RECEIVED", "error": "", "kind": "AUTO_RESPONSE", "outgoing": true, "reviewRequest": false, "type": "SMS", "readDate": 0, "respondedDate": 0, "sentDate": 1576783233546, "attachments": []}, {"id": 4620511, "body": "test sms,test sms", "conversationId": 1005672, "locationId": 2045, "contactId": 12792806, "assignedUserId": 17297, "status": "DELIVERED", "error": "", "kind": "API", "outgoing": true, "reviewRequest": false, "type": "SMS", "readDate": 0, "respondedDate": 0, "sentDate": 1577987093930, "attachments": []}]}'

输出为:

{    
    4461048: 'Mnow test test', 
    4461049: 'THIS NUMBER DOES NOT CURRENTLY ACCEPT TEXT MESSAGES PLEASE CALL (716) 444-4444 TO WORK WITH ONE OF OUR INTAKE SPECIALISTS', 
    4620511: 'test sms,test sms'
}

答案 1 :(得分:0)

类似的事情可能会起作用:

# initialize variables:
inbound_dictionary_list = []

for item in inbound['messages']:

    # extract key, value:
    key = item['conversationId']
    value = item['body']

    # pack into dictionary object:
    parsed_item = {key:value}

    # update existing dictionary:
    inbound_dictionary_list.append(parsed_item)

详细信息:https://www.w3schools.com/python/ref_dictionary_update.asp