如何在python中嵌套json?

如何在python中嵌套json?,python,json,django,Python,Json,Django,我正在开发django应用程序,需要将数据作为json从django sql传递到javascript。我正在努力做一件事。我可以嵌套一级,但我不能嵌套第二级。在嵌套第二层时,json会根据指定替换值,而不是附加值。我的目标是获得以下效果: 日期>[time1>[1,2,3],time2>[1,2,3]],日期2>[time1>[1,2,3]]等 在这一刻,我不能超过一次:(感谢您的帮助 reserved = Reservation.objects.get(customer=user_info)

我正在开发django应用程序,需要将数据作为json从django sql传递到javascript。我正在努力做一件事。我可以嵌套一级,但我不能嵌套第二级。在嵌套第二层时,json会根据指定替换值,而不是附加值。我的目标是获得以下效果:

日期>[time1>[1,2,3],time2>[1,2,3]],日期2>[time1>[1,2,3]]等

在这一刻,我不能超过一次:(感谢您的帮助

reserved = Reservation.objects.get(customer=user_info)
        for x in reserved.reservations.all():
            title = x.movie.title
            image = x.movie.image
            date = str(x.date)
            time = str(x.time)
            dates = {date:{}}
            times = {time:{}}
            if title in dic:
                pass
            else:
                dic[title] = {"image":str(image), "dates":{}}
            for y in x.seats.all():
                seats = {y.row:y.number}
                times[time].update(seats)
            dates[date].update(times)
            dic[title]["dates"].update(dates)

你会以JSON格式显示你的预期输出吗?@BoseongChoi刚刚编辑过。应该还有2个小时,但如上所述,JSON会根据itteration替换值,而不是附加当前的itteration。我不明白。你想要的最终结果是什么?它看起来像什么?以JSON格式显示给我们。@BoseongChoi我发布了“当前结果”下的照片,并明确指出每个日期下应该有更多的“小时”。此时,我只能嵌套一个小时,因为我的循环覆盖了结果,而不是附加它。