這可能是一個真正的菜鳥問題。
感謝您的幫助。
我正在接收具有以下結構的 json 有效負載
{
"response": true,
"error": null,
"msg": null,
"data": {
"slots": {
"04/26/2022": [
{
"0": 1650958200000,
"1": 19800,
"3": "1:00 PM",
"4": "26/Apr/2022 13:00 IST"
},
{
"0": 1650960000000,
"1": 19800,
"3": "1:30 PM",
"4": "26/Apr/2022 13:30 IST"
},
{
"0": 1650963600000,
"1": 19800,
"3": "2:30 PM",
"4": "26/Apr/2022 14:30 IST"
},
{
"0": 1650965400000,
"1": 19800,
"3": "3:00 PM",
"4": "26/Apr/2022 15:00 IST"
},
{
"0": 1650967200000,
"1": 19800,
"3": "3:30 PM",
"4": "26/Apr/2022 15:30 IST"
}
],
"04/24/2022": [],
"04/25/2022": [],
"04/23/2022": []
}
}
}
我想創建一個具有“0”值陣列的新物件。輸出應該是
{
"slots": [
1650958200000,
1650960000000,
1650963600000,
1650965400000,
1650967200000
]
}
我該怎么做呢?
uj5u.com熱心網友回復:
這是一個直截了當的方法:
slots = payload["data"]["slots"] # Assumes payload has been converted from JSON to Python
slots_obj = {"slots": [d["0"] for _, lst in slots.items() for d in lst]}
輸出:
{'slots': [1650958200000,
1650960000000,
1650963600000,
1650965400000,
1650967200000,
16539137693178]}
如果您還沒有,請使用json.loads將 JSON 轉換為 Python 字典:
import json
payload = json.loads(payload)
上面的解決方案使用了所謂的串列理解。相當于一個for回圈,可能更容易理解:
timestamps = [] # Initialize an empty list to accumulate the Unix timestamps
# Iterate over each date, array pair in the slots dictionary using slots.items()
for _, slot_array in slots.items():
# Iterate over each inner dictionary
for slot_dict in slot_array:
# Grab the value at the "0" and append it to the timestamps accumulator
timestamps.append(slot_dict["0"])
# Finally create the new object, a dictionary with one key, "slots"
slots_obj = {"slots": timestamps}
uj5u.com熱心網友回復:
使用's 方法json中的模塊ddejohn將比此處顯示的方法更有效。但是,我認為查看獲得所需輸出的其他方法對您很有用。
如果上面是一個字串,比如說,,s你可以findall()從re模塊中使用來提取這些數字。
import re
p = re.compile(r'(?<="0": )\d (?=,)')
d = {'slots': [int(x) for x in p.findall(s)]}
print(d)
輸出
{'slots': [1650958200000,
1650960000000,
1650963600000,
1650965400000,
1650967200000]}
正則運算式r'(?<="0": )\d (?=,)'匹配一個或多個前面"0": 和后面跟著 a 的數字,。findall(s)將字串中此運算式的所有(非重疊)匹配項s作為字串串列回傳。特別是,p.findall(s)回傳串列
['1650958200000',
'1650960000000',
'1650963600000',
'1650965400000',
'1650967200000']
[int(x) for x in p.findall(s)]通過獲取回傳的串列的每個元素p.findall(s)并將其轉換為整數來創建一個新串列。
計時
像 in 的方法那樣使用json模塊ddejohn會更有效。
# ddejohn
6.14 μs ± 41 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
# oda
14.6 μs ± 76.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
uj5u.com熱心網友回復:
我做了下一個程式,希望對你有用。
但是您應該記住,您可能會丟失重要資訊
它的作業原理在程式中的解釋
import json
dicc={}
# We open the file
with open('data.json', 'r') as f:
data = json.JSONDecoder().decode(f.read())
# We get the data
slots=data['data']['slots']
# We travel to the slots and we get the key
for slot in slots:
# We get the data and put them in the dictionary
for key in slots[slot]:
# If slots key is not created, we create it
if "slots" not in dicc:
# We create the list that will contain the data
dicc["slots"]=[]
#We add the data to the list in the dictionary
dicc["slots"].append(key['0'])
new_json=json.dumps(dicc)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/462977.html
上一篇:For回圈-我只能輸入1個整數
