在Json结构上循环以获得多个输出,我得到了一个,为什么?

2024-03-29 13:58:38 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我是json的新面孔,我想问一些关于它的问题,不管怎样,我的代码是:

for key in result1date.keys():
    key = sorted(result1date.keys())
    currentdate = key

    print currentdate

    result1 = firebase.get('/Rooms/Room1/' + currentdate + '/Inspection/Scan-in/Inspector/', None)
    result2 = firebase.get('/Rooms/Room1/' + currentdate + '/Inspection/Scan-out/Inspector/', None)
    print result1

    print currentdate

    for key in result1.keys():
         inspector = key
         timeout = result2[inspector]["Time"]

    # for key in result1date.keys():
    #    Date = key

    result1datetime = firebase.get('/Rooms/Room1/'+ currentdate +'/Inspection/Scan-in/Inspector/'+ inspector +'/', None)

    for key in result1datetime.keys():
        key = sorted(result1datetime.keys())[-2]
        time = key
        print time

     print time

     ch1 = result1datetime[time]["Checklist"]["Entrance louver clean and dust-free"]
     # ch2 = result1datetime[time]["Checklist"]["Room plate number – clean and well-polished"]
     ch3 = result1datetime[time]["Checklist"]["Appearance door surface- in good condition"]   
     # ch4 = result1datetime[time]["Checklist"]["Let the door close by itself to test the door closure – in working order"]
     ch5 = result1datetime[time]["Checklist"]["Eye viewer and fire escape plan in order"]
     ch6 = result1datetime[time]["Checklist"]["Privacy Sign or Make Up Room Sign"]
     # ch7 = result1datetime[time]["Checklist"]["Key card holder – in working order"]
     ch8 = result1datetime[time]["Checklist"]["Switches at the entrance working correctly"]

    #CLOSET
    #ch9 = result1datetime[time]["Checklist"]["Let the door close by itself to test the door closure – in working order"] 
     RESERVED FOR DOOR IN WORKING CONDITION
     ch10 = result1datetime[time]["Checklist"]["Lights in working order"] 

    # items below are sufficient
     ch11 = result1datetime[time]["Checklist"]["6 Hangers?"]
     ch12 = result1datetime[time]["Checklist"]["2 bathrobes?"]
     ch13 = result1datetime[time]["Checklist"]["2 pairs of slippers?"]
     ch14 = result1datetime[time]["Checklist"]["1 set of iron and board?"]
     ch15 = result1datetime[time]["Checklist"]["Elsafe open or working?"]
     ch16 = result1datetime[time]["Checklist"]["1 set of laundry list and bag?"]
     ch17 = result1datetime[time]["Checklist"]["1 extra pillow with pillow cover?"] 
     #ch18 = result1datetime[time]["Checklist"]["Luggage bench fabric top is clean"]#DINING DRESS CODE
     #ch19 = result1datetime[time]["Checklist"]["Luggage bench fabric top is clean"] #FLASHLIGHT

     #LUGGAGE AND LUNCH BREAK
      ch20 = result1datetime[time]["Checklist"]["Luggage  bench fabric top is clean"]
      # ch21 = result1datetime[time]["Checklist"]["Drawers – clean and dust-free"]

      #MINIBAR
      #ch22 = result1datetime[time]["Checklist"]["Luggage bench fabric top is clean"]#Arrangement of the items is neat & clean.
      #ch23 = result1datetime[time]["Checklist"]["Luggage bench fabric top is clean"]#Ensure the items below are sufficient
      ch24 = result1datetime[time]["Checklist"]["2 coke, 2  sprite, 1 C2 lemon, 1 C2 apple, 1 pineapple juice, 1 orange juice, 1 mineral water, 2 San Mig light, 2 pale pilsen?"]
      ch25 = result1datetime[time]["Checklist"]["1 pringles, 1 cashew nut, 1 cup noodles (placed in the coffee tray on the writing desk)?"]
      ch26 = result1datetime[time]["Checklist"]["Fridge is cold and clean"]

我有三次约会所以我在它们身上循环,希望得到相同的输出,但其中三个日期不同。这是我的firebase结构:

https://i.stack.imgur.com/DfYSP.png

但我只有一个。当我用这部分代码循环所有的键时

^{pr2}$

下面的人应该跟着它。感谢任何帮助。我想知道为什么会发生这种情况,希望能找到解决办法或建议。在

我想得到这样的输出:

[DATE 1 HERE with all the details within its branches]
[DATE 2 HERE with all the details within its branches]
[Date 3 HERE with all the details within its branches]

Tags: andthekeyincleantimeisorder
2条回答

我对python或json不太熟悉,所以这在您的情况下可能不起作用,但我尽力构建了它,希望能对您有所帮助

如果Json文件具有固定的稳定结构,或者如果值的格式非常类似,我想您可以这样做:

Json示例dict

firebase_json = {
"Rooms": {
    "Room1": {
        "2017-11-29": {
            "Status": ["unoccupied"],
            "Inspection": ["yes"],
            "Scan-in": ["no"],
            "Scan-out": ["yes"]
            },
        "2017-12-05": {
            "Status": ["occupied"],
            "Inspection": ["no"],
            "Scan-in": ["yes"],
            "Scan-out": ["no"]
        },
    },
    "Room2": {
        "2017-11-02": {
            "Status": ["unoccupied"],
            "Inspection": ["yes"],
            "Scan-in": ["no"],
            "Scan-out": ["yes"]
        },
        "2017-12-01": {
            "Status": ["occupied"],
            "Inspection": ["no"],
            "Scan-in": ["yes"],
            "Scan-out": ["no"]
        }
    }
}}

样本选择

^{pr2}$

也许还有更好的办法,但是正如我所说。在

这看起来不对:

for key in result1datetime.keys():
    key = sorted(result1datetime.keys())[-2]
    time = key
    print time

您在for循环中迭代您的键,但是在下一行(sorted(result1datetime.keys())[-2])中总是得到相同的键。在

你为什么一开始就加上那句话?这样,你就可以(三个中)得到中间键。在

也许您想对返回的键进行排序(key = sorted(key)[-2])。你的结构图像显示得不够深,不知道在这个层次上会发生什么。在

无论如何,我建议您不要像这样在循环中重用key变量。这会让事情变得难以理解。请注意,问题是将它重置为一个固定元素,该元素与您最初循环的iterable相同。在

相关问题 更多 >