以下是我的数据:
{
"data": [
{
"date": 1577836800000,
"@NOTIFICATION_SENT": 62629,
"@NOTIFICATION_OPENED": 404
},
{
"date": 1577923200000,
"@NOTIFICATION_OPENED": 734
}
]
}
如何添加所有@NOTIFICATION\u OPENED”键以在同一字符串中打开所有notif?如果不可能,如何仅选择第一个键“@NOTIFICATION\u OPENED”
使用我的代码,我打印最后一个重复密钥的值
这是我的密码:
def create_json(id, notificationSent, notificationOpened):
return {(id):{
'id': id,
'notificationSent': notificationSent,
'notificationOpened': notificationOpened,
}}
statUrl = 'myapiurl'
with urlopen (statUrl) as response: sourcedata = response.read()
statdata = json.loads (sourcedata)
def push_data():
newJsonx = dict()
for item in data["data"]:
for item in statdata["data"]:
try:
notificationOpened = item["@NOTIFICATION_OPENED"]
except:
notificationOpened = '0'
print(notificationOpened)
try:
notificationSent = item["@NOTIFICATION_SENT"]
except:
notificationSent = '0'
# JSON DATA
newJson = create_json(notificationSent, notificationOpened)
newJsonx.update(newJson)
with open('myfile.json', 'w', encoding='utf-8') as json_file:
json.dump(newJsonx, json_file, ensure_ascii=False, sort_keys=True, indent=2)
print('JSON: %s' % json.dumps(newJsonx, sort_keys=True, indent=2))
push_data()
1138
这里是原始代码的变体,它将创建您要查找的词典
这使得:
相关问题 更多 >
编程相关推荐