2017-12-06 4 views
4

こんにちは、私はJSONに新しい顔だと私はそれについて何かをお聞きしたい、とにかくここに私のコードです:Json構造のルーピング複数の出力を得るには、私は1つ、なぜですか?

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"] 

私は3日を持っています。 'currentdate'なので、私は同じ出力を得ることを望んでいるが、それらのすべてをループしていたが、異なる3つの日付を持っていた。ここに私のfirebase構造です:

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

しかし、私は一つだけを取得しています。コードのこの部分を使用してすべてのキーをループしたとき

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

以下のものがそれに従っているはずです。どんな助けもありがとうございます。私はなぜこれが起きているのか、うまくいけば解決策か示唆を知りたい。

私はこのような何かの出力を取得したい:

[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] 
+3

例をもっと小さくする必要があります。 – erip

答えて

0

私は、これはあなたのケースでは動作しない場合がありますPythonやJSONとその慣れていないんだけど、私の最高の建物これをしなかった、それはことを願っていくつかの助けになることが

JSONファイルが固定的に安定な構造を持っているか、値がさえ全く同様にフォーマットされている場合、私はあなたがこれを行うことが考えていた場合は、次の

辞書としてJSONサンプル:

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"] 
     } 
    } 
}} 

サンプルの選択

room = input("Room?",) 
for a in firebase_json: 
    for b in firebase_json[a]: 
     if b != room: #select by given room. 
      continue 
     print(b) 
     for c in firebase_json[a][b]: 
      print(" ", c) # if selection by date add previous != statement here 
      for d in firebase_json[a][b][c]: 
       for e in firebase_json[a][b][c][d]: 
        print(" ", d, ":", e) 

あり、これを行うための良い方法はおそらくですが、私が言ったように。

1

これは間違っているになります。

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

あなたはforループの中であなたの鍵を反復処理しているが、その後、あなたは常に次の行(sorted(result1datetime.keys())[-2])で同じキーを取得します。

なぜ最初にその行を追加しましたか?この方法で、あなたはいつも中間鍵(3つのうち)を得ています。

代わりに、返されたキーを並べ替えることをお勧めします(key = sorted(key)[-2])。あなたの構造イメージは、そのレベルで何を期待するかを知るのに十分深くは見えません。

いずれにしても、そのようなループ内にkey変数を再利用しないことをお勧めします。物事を理解するのが難しくなります。そして、問題は、最初にループしていたのと同じ繰り返し可能な要素の固定要素に対してそれをリセットすることです。

関連する問題