python-3.x 如何将字符串转换为JSON格式的列表

44u64gxh  于 2023-11-20  发布在  Python
关注(0)|答案(2)|浏览(87)
import json    
accept_suggested_timings = """{"2023-11-20 00:00:00":[{"startDateTime":"2023-11-20 14:00:00","endDateTime":"2023-11-20 16:00:00"},{"startDateTime":"2023-11-20 16:00:00","endDateTime":"2023-11-20 18:00:00"},{"startDateTime":"2023-11-20 18:00:00","endDateTime":"2023-11-20 20:00:00"},{"startDateTime":"2023-11-20 20:00:00","endDateTime":"2023-11-20 22:00:00"}],"2023-11-21 00:00:00":[{"startDateTime":"2023-11-21 14:00:00","endDateTime":"2023-11-21 16:00:00"},{"startDateTime":"2023-11-21 16:00:00","endDateTime":"2023-11-21 18:00:00"},{"startDateTime":"2023-11-21 18:00:00","endDateTime":"2023-11-21 20:00:00"},{"startDateTime":"2023-11-21 20:00:00","endDateTime":"2023-11-21 22:00:00"}],"2023-11-22 00:00:00":[{"startDateTime":"2023-11-22 14:00:00","endDateTime":"2023-11-22 16:00:00"},{"startDateTime":"2023-11-22 16:00:00","endDateTime":"2023-11-22 18:00:00"},{"startDateTime":"2023-11-22 18:00:00","endDateTime":"2023-11-22 20:00:00"},{"startDateTime":"2023-11-22 20:00:00","endDateTime":"2023-11-22 22:00:00"}],"2023-11-23 00:00:00":[{"startDateTime":"2023-11-23 14:00:00","endDateTime":"2023-11-23 16:00:00"},{"startDateTime":"2023-11-23 16:00:00","endDateTime":"2023-11-23 18:00:00"},{"startDateTime":"2023-11-23 18:00:00","endDateTime":"2023-11-23 20:00:00"},{"startDateTime":"2023-11-23 20:00:00","endDateTime":"2023-11-23 22:00:00"}],"2023-11-24 00:00:00":[{"startDateTime":"2023-11-24 14:00:00","endDateTime":"2023-11-24 16:00:00"},{"startDateTime":"2023-11-24 16:00:00","endDateTime":"2023-11-24 18:00:00"},{"startDateTime":"2023-11-24 18:00:00","endDateTime":"2023-11-24 20:00:00"},{"startDateTime":"2023-11-24 20:00:00","endDateTime":"2023-11-24 22:00:00"}],"2023-11-27 00:00:00":[{"startDateTime":"2023-11-27 14:00:00","endDateTime":"2023-11-27 16:00:00"},{"startDateTime":"2023-11-27 16:00:00","endDateTime":"2023-11-27 18:00:00"},{"startDateTime":"2023-11-27 18:00:00","endDateTime":"2023-11-27 20:00:00"},{"startDateTime":"2023-11-27 20:00:00","endDateTime":"2023-11-27 22:00:00"}],"2023-11-28 00:00:00":[{"startDateTime":"2023-11-28 14:00:00","endDateTime":"2023-11-28 16:00:00"},{"startDateTime":"2023-11-28 16:00:00","endDateTime":"2023-11-28 18:00:00"},{"startDateTime":"2023-11-28 18:00:00","endDateTime":"2023-11-28 20:00:00"},{"startDateTime":"2023-11-28 20:00:00","endDateTime":"2023-11-28 22:00:00"}],"2023-11-29 00:00:00":[{"startDateTime":"2023-11-29 14:00:00","endDateTime":"2023-11-29 16:00:00"},{"startDateTime":"2023-11-29 16:00:00","endDateTime":"2023-11-29 18:00:00"},{"startDateTime":"2023-11-29 18:00:00","endDateTime":"2023-11-29 20:00:00"},{"startDateTime":"2023-11-29 20:00:00","endDateTime":"2023-11-29 22:00:00"}],"2023-11-30 00:00:00":[{"startDateTime":"2023-11-30 14:00:00","endDateTime":"2023-11-30 16:00:00"},{"startDateTime":"2023-11-30 16:00:00","endDateTime":"2023-11-30 18:00:00"},{"startDateTime":"2023-11-30 18:00:00","endDateTime":"2023-11-30 20:00:00"},{"startDateTime":"2023-11-30 20:00:00","endDateTime":"2023-11-30 22:00:00"}]}"""
print(list(accept_suggested_timings))
a = json.loads(accept_suggested_timings)
b = list(a)
print(b)

字符串
有没有办法把string转换成json?
Required solution: [ {"2023-11-20 00:00:00":[{"startDateTime":"2023-11-20 14:00:00","endDateTime":"2023-11-20 16:00:00"},{"startDateTime":"2023-11-20 16:00:00","endDateTime":"2023-11-20 18:00:00"},{"startDateTime":"2023-11-20 18:00:00","endDateTime":"2023-11-20 20:00:00"},{"startDateTime":"2023-11-20 20:00:00","endDateTime":"2023-11-20 22:00:00"}],"2023-11-21 00:00:00":[{"startDateTime":"2023-11-21 14:00:00","endDateTime":"2023-11-21 16:00:00"},{"startDateTime":"2023-11-21 16:00:00","endDateTime":"2023-11-21 18:00:00"},{"startDateTime":"2023-11-21 18:00:00","endDateTime":"2023-11-21 20:00:00"},{"startDateTime":"2023-11-21 20:00:00","endDateTime":"2023-11-21 22:00:00"}],"2023-11-22 00:00:00":[{"startDateTime":"2023-11-22 14:00:00","endDateTime":"2023-11-22 16:00:00"},{"startDateTime":"2023-11-22 16:00:00","endDateTime":"2023-11-22 18:00:00"},{"startDateTime":"2023-11-22 18:00:00","endDateTime":"2023-11-22 20:00:00"},{"startDateTime":"2023-11-22 20:00:00","endDateTime":"2023-11-22 22:00:00"}],"2023-11-23 00:00:00":[{"startDateTime":"2023-11-23 14:00:00","endDateTime":"2023-11-23 16:00:00"},{"startDateTime":"2023-11-23 16:00:00","endDateTime":"2023-11-23 18:00:00"},{"startDateTime":"2023-11-23 18:00:00","endDateTime":"2023-11-23 20:00:00"},{"startDateTime":"2023-11-23 20:00:00","endDateTime":"2023-11-23 22:00:00"}],"2023-11-24 00:00:00":[{"startDateTime":"2023-11-24 14:00:00","endDateTime":"2023-11-24 16:00:00"},{"startDateTime":"2023-11-24 16:00:00","endDateTime":"2023-11-24 18:00:00"},{"startDateTime":"2023-11-24 18:00:00","endDateTime":"2023-11-24 20:00:00"},{"startDateTime":"2023-11-24 20:00:00","endDateTime":"2023-11-24 22:00:00"}],"2023-11-27 00:00:00":[{"startDateTime":"2023-11-27 14:00:00","endDateTime":"2023-11-27 16:00:00"},{"startDateTime":"2023-11-27 16:00:00","endDateTime":"2023-11-27 18:00:00"},{"startDateTime":"2023-11-27 18:00:00","endDateTime":"2023-11-27 20:00:00"},{"startDateTime":"2023-11-27 20:00:00","endDateTime":"2023-11-27 22:00:00"}],"2023-11-28 00:00:00":[{"startDateTime":"2023-11-28 14:00:00","endDateTime":"2023-11-28 16:00:00"},{"startDateTime":"2023-11-28 16:00:00","endDateTime":"2023-11-28 18:00:00"},{"startDateTime":"2023-11-28 18:00:00","endDateTime":"2023-11-28 20:00:00"},{"startDateTime":"2023-11-28 20:00:00","endDateTime":"2023-11-28 22:00:00"}],"2023-11-29 00:00:00":[{"startDateTime":"2023-11-29 14:00:00","endDateTime":"2023-11-29 16:00:00"},{"startDateTime":"2023-11-29 16:00:00","endDateTime":"2023-11-29 18:00:00"},{"startDateTime":"2023-11-29 18:00:00","endDateTime":"2023-11-29 20:00:00"},{"startDateTime":"2023-11-29 20:00:00","endDateTime":"2023-11-29 22:00:00"}],"2023-11-30 00:00:00":[{"startDateTime":"2023-11-30 14:00:00","endDateTime":"2023-11-30 16:00:00"},{"startDateTime":"2023-11-30 16:00:00","endDateTime":"2023-11-30 18:00:00"},{"startDateTime":"2023-11-30 18:00:00","endDateTime":"2023-11-30 20:00:00"},{"startDateTime":"2023-11-30 20:00:00","endDateTime":"2023-11-30 22:00:00"}]} ]

lb3vh1jj

lb3vh1jj1#

你可以使用list comprehension来实现:
代码:

import json

accept_suggested_timings = """{"2023-11-20 00:00:00":[{"startDateTime":"2023-11-20 14:00:00","endDateTime":"2023-11-20 16:00:00"},{"startDateTime":"2023-11-20 16:00:00","endDateTime":"2023-11-20 18:00:00"},{"startDateTime":"2023-11-20 18:00:00","endDateTime":"2023-11-20 20:00:00"},{"startDateTime":"2023-11-20 20:00:00","endDateTime":"2023-11-20 22:00:00"}],"2023-11-21 00:00:00":[{"startDateTime":"2023-11-21 14:00:00","endDateTime":"2023-11-21 16:00:00"},{"startDateTime":"2023-11-21 16:00:00","endDateTime":"2023-11-21 18:00:00"},{"startDateTime":"2023-11-21 18:00:00","endDateTime":"2023-11-21 20:00:00"},{"startDateTime":"2023-11-21 20:00:00","endDateTime":"2023-11-21 22:00:00"}],"2023-11-22 00:00:00":[{"startDateTime":"2023-11-22 14:00:00","endDateTime":"2023-11-22 16:00:00"},{"startDateTime":"2023-11-22 16:00:00","endDateTime":"2023-11-22 18:00:00"},{"startDateTime":"2023-11-22 18:00:00","endDateTime":"2023-11-22 20:00:00"},{"startDateTime":"2023-11-22 20:00:00","endDateTime":"2023-11-22 22:00:00"}],"2023-11-23 00:00:00":[{"startDateTime":"2023-11-23 14:00:00","endDateTime":"2023-11-23 16:00:00"},{"startDateTime":"2023-11-23 16:00:00","endDateTime":"2023-11-23 18:00:00"},{"startDateTime":"2023-11-23 18:00:00","endDateTime":"2023-11-23 20:00:00"},{"startDateTime":"2023-11-23 20:00:00","endDateTime":"2023-11-23 22:00:00"}],"2023-11-24 00:00:00":[{"startDateTime":"2023-11-24 14:00:00","endDateTime":"2023-11-24 16:00:00"},{"startDateTime":"2023-11-24 16:00:00","endDateTime":"2023-11-24 18:00:00"},{"startDateTime":"2023-11-24 18:00:00","endDateTime":"2023-11-24 20:00:00"},{"startDateTime":"2023-11-24 20:00:00","endDateTime":"2023-11-24 22:00:00"}],"2023-11-27 00:00:00":[{"startDateTime":"2023-11-27 14:00:00","endDateTime":"2023-11-27 16:00:00"},{"startDateTime":"2023-11-27 16:00:00","endDateTime":"2023-11-27 18:00:00"},{"startDateTime":"2023-11-27 18:00:00","endDateTime":"2023-11-27 20:00:00"},{"startDateTime":"2023-11-27 20:00:00","endDateTime":"2023-11-27 22:00:00"}],"2023-11-28 00:00:00":[{"startDateTime":"2023-11-28 14:00:00","endDateTime":"2023-11-28 16:00:00"},{"startDateTime":"2023-11-28 16:00:00","endDateTime":"2023-11-28 18:00:00"},{"startDateTime":"2023-11-28 18:00:00","endDateTime":"2023-11-28 20:00:00"},{"startDateTime":"2023-11-28 20:00:00","endDateTime":"2023-11-28 22:00:00"}],"2023-11-29 00:00:00":[{"startDateTime":"2023-11-29 14:00:00","endDateTime":"2023-11-29 16:00:00"},{"startDateTime":"2023-11-29 16:00:00","endDateTime":"2023-11-29 18:00:00"},{"startDateTime":"2023-11-29 18:00:00","endDateTime":"2023-11-29 20:00:00"},{"startDateTime":"2023-11-29 20:00:00","endDateTime":"2023-11-29 22:00:00"}],"2023-11-30 00:00:00":[{"startDateTime":"2023-11-30 14:00:00","endDateTime":"2023-11-30 16:00:00"},{"startDateTime":"2023-11-30 16:00:00","endDateTime":"2023-11-30 18:00:00"},{"startDateTime":"2023-11-30 18:00:00","endDateTime":"2023-11-30 20:00:00"},{"startDateTime":"2023-11-30 20:00:00","endDateTime":"2023-11-30 22:00:00"}]}"""

a = [json.loads(accept_suggested_timings) for i in accept_suggested_timings]

print(a) # your list

字符串
检查output here

x8goxv8g

x8goxv8g2#

如果你只是想要日期,检查这个:

import json

accept_suggested_timings = """{"2023-11-20 00:00:00":[{"startDateTime":"2023-11-20 14:00:00","endDateTime":"2023-11-20 16:00:00"},{"startDateTime":"2023-11-20 16:00:00","endDateTime":"2023-11-20 18:00:00"},{"startDateTime":"2023-11-20 18:00:00","endDateTime":"2023-11-20 20:00:00"},{"startDateTime":"2023-11-20 20:00:00","endDateTime":"2023-11-20 22:00:00"}],"2023-11-21 00:00:00":[{"startDateTime":"2023-11-21 14:00:00","endDateTime":"2023-11-21 16:00:00"},{"startDateTime":"2023-11-21 16:00:00","endDateTime":"2023-11-21 18:00:00"},{"startDateTime":"2023-11-21 18:00:00","endDateTime":"2023-11-21 20:00:00"},{"startDateTime":"2023-11-21 20:00:00","endDateTime":"2023-11-21 22:00:00"}],"2023-11-22 00:00:00":[{"startDateTime":"2023-11-22 14:00:00","endDateTime":"2023-11-22 16:00:00"},{"startDateTime":"2023-11-22 16:00:00","endDateTime":"2023-11-22 18:00:00"},{"startDateTime":"2023-11-22 18:00:00","endDateTime":"2023-11-22 20:00:00"},{"startDateTime":"2023-11-22 20:00:00","endDateTime":"2023-11-22 22:00:00"}],"2023-11-23 00:00:00":[{"startDateTime":"2023-11-23 14:00:00","endDateTime":"2023-11-23 16:00:00"},{"startDateTime":"2023-11-23 16:00:00","endDateTime":"2023-11-23 18:00:00"},{"startDateTime":"2023-11-23 18:00:00","endDateTime":"2023-11-23 20:00:00"},{"startDateTime":"2023-11-23 20:00:00","endDateTime":"2023-11-23 22:00:00"}],"2023-11-24 00:00:00":[{"startDateTime":"2023-11-24 14:00:00","endDateTime":"2023-11-24 16:00:00"},{"startDateTime":"2023-11-24 16:00:00","endDateTime":"2023-11-24 18:00:00"},{"startDateTime":"2023-11-24 18:00:00","endDateTime":"2023-11-24 20:00:00"},{"startDateTime":"2023-11-24 20:00:00","endDateTime":"2023-11-24 22:00:00"}],"2023-11-27 00:00:00":[{"startDateTime":"2023-11-27 14:00:00","endDateTime":"2023-11-27 16:00:00"},{"startDateTime":"2023-11-27 16:00:00","endDateTime":"2023-11-27 18:00:00"},{"startDateTime":"2023-11-27 18:00:00","endDateTime":"2023-11-27 20:00:00"},{"startDateTime":"2023-11-27 20:00:00","endDateTime":"2023-11-27 22:00:00"}],"2023-11-28 00:00:00":[{"startDateTime":"2023-11-28 14:00:00","endDateTime":"2023-11-28 16:00:00"},{"startDateTime":"2023-11-28 16:00:00","endDateTime":"2023-11-28 18:00:00"},{"startDateTime":"2023-11-28 18:00:00","endDateTime":"2023-11-28 20:00:00"},{"startDateTime":"2023-11-28 20:00:00","endDateTime":"2023-11-28 22:00:00"}],"2023-11-29 00:00:00":[{"startDateTime":"2023-11-29 14:00:00","endDateTime":"2023-11-29 16:00:00"},{"startDateTime":"2023-11-29 16:00:00","endDateTime":"2023-11-29 18:00:00"},{"startDateTime":"2023-11-29 18:00:00","endDateTime":"2023-11-29 20:00:00"},{"startDateTime":"2023-11-29 20:00:00","endDateTime":"2023-11-29 22:00:00"}],"2023-11-30 00:00:00":[{"startDateTime":"2023-11-30 14:00:00","endDateTime":"2023-11-30 16:00:00"},{"startDateTime":"2023-11-30 16:00:00","endDateTime":"2023-11-30 18:00:00"},{"startDateTime":"2023-11-30 18:00:00","endDateTime":"2023-11-30 20:00:00"},{"startDateTime":"2023-11-30 20:00:00","endDateTime":"2023-11-30 22:00:00"}]}"""

a = [json.loads(accept_suggested_timings) for i in accept_suggested_timings]

my_dates = []
count = 0
for i in a[0]:
    dates = i.split(" ")[0]

    my_dates.append(dates)
    count += 1
    if count == 3:
        break

print(my_dates)

字符串
output of the code

相关问题