2015-11-04 93 views
0

我處理一個JSON結構,輸出到我在結構,像這樣的JSON數組數據:拼合在python

{ 
    "time":"2015-10-20T20:15:00.847Z", 
    "name":"meta.response.ean", 
    "level":"info", 
    "data1":{ 
    "HotelListResponse":{ 
     "customerSessionId":"0AB29024-F6D4-3915-0862-DB3FD1904C5A", 
     "numberOfRoomsRequested":1, 
     "moreResultsAvailable":true, 
     "cacheKey":"-705f6d43:15086db3fd1:-4c58", 
     "cacheLocation":"10.178.144.36:7300", 
     "HotelList":{ 
      "@size":2, 
      "@activePropertyCount":2, 
      "HotelSummary":[ 
       { 
       "hotelId":132684, 
       "city":"Seattle", 
       "highRate":159.0, 
       "lowRate":159.0, 
       "rateCurrencyCode":"USD", 
       "RoomRateDetailsList":{ 
        "RoomRateDetails":{ 
         "roomTypeCode":10351, 
         "rateCode":10351, 
         "roomDescription":"Standard Room, 1 Queen Bed", 
         "RateInfos":{ 
          "RateInfo":{ 
          "@promo":false, 
          "ChargeableRateInfo":{ 
           "@averageBaseRate":159.0, 
           "@averageRate":159.0, 
           "@currencyCode":"USD", 
           "@nightlyRateTotal":159.0, 
           "@surchargeTotal":26.81, 
           "@total":185.81 
          } 
          } 
         } 
        } 
       } 
       }, 
       { 
       "hotelId":263664, 
       "city":"Las Vegas", 
       "highRate":135.0, 
       "lowRate":94.5, 
       "rateCurrencyCode":"USD", 
       "RoomRateDetailsList":{ 
        "RoomRateDetails":{ 
         "roomTypeCode":373685, 
         "rateCode":1238953, 
         "roomDescription":"Standard Room, 1 King Bed", 
         "RateInfos":{ 
          "RateInfo":{ 
          "@promo":true, 
          "ChargeableRateInfo":{ 
           "@averageBaseRate":135.0, 
           "@averageRate":94.5, 
           "@currencyCode":"USD", 
           "@nightlyRateTotal":94.5, 
           "@surchargeTotal":9.45, 
           "@total":103.95 
          } 
          } 
         } 
        } 
       } 
       } 
      ] 
     } 
    } 
    }, 
    "context":{ 
    "X-Request-Id":"dca47992-b6cc-4b87-956c-90523c0bf3bb", 
    "host":"getaways-search-app2", 
    "thread":"http-nio-80-exec-12" 
    } 
} 

正如你所看到的,這些嵌套數組。有很多關於遞歸平坦化的討論。 「HotelSummary」下的數組無法平鋪。有任何想法嗎?

+1

爲什麼你無法平整陣列?你嘗試了什麼,什麼不起作用? – ekhumoro

回答

0
{ 
    "HotelListResponse":{ 
     "customerSessionId":"0AB29024-F6D4-3915-0862-DB3FD1904C5A", 
     "numberOfRoomsRequested":1, 
     "moreResultsAvailable":"true", 
     "cacheKey":"-705f6d43:15086db3fd1:-4c58", 
     "cacheLocation":"10.178.144.36:7300", 
     "HotelList":{ 
     "size":2, 
     "activePropertyCount":2, 
     "HotelSummary":[ 
      { 
       "hotelId":132684, 
       "city":"Seattle", 
       "highRate":159.0, 
       "lowRate":159.0, 
       "rateCurrencyCode":"USD", 
       "RoomRateDetailsList":{ 
        "RoomRateDetails":{ 
        "roomTypeCode":10351, 
        "rateCode":10351, 
        "roomDescription":"Standard Room, 1 Queen Bed", 
        "RateInfos":{ 
         "RateInfo":{ 
          "promo":"false", 
          "ChargeableRateInfo":{ 
           "averageBaseRate":159.0, 
           "averageRate":159.0, 
           "currencyCode":"USD", 
           "nightlyRateTotal":159.0, 
           "surchargeTotal":26.81, 
           "total":185.81 
          } 
         } 
        } 
        } 
       } 
      }, 
      { 
       "hotelId":263664, 
       "city":"Las Vegas", 
       "highRate":135.0, 
       "lowRate":94.5, 
       "rateCurrencyCode":"USD", 
       "RoomRateDetailsList":{ 
        "RoomRateDetails":{ 
        "roomTypeCode":373685, 
        "rateCode":1238953, 
        "roomDescription":"Standard Room, 1 King Bed", 
        "RateInfos":{ 
         "RateInfo":{ 
          "promo":"true", 
          "ChargeableRateInfo":{ 
           "averageBaseRate":135.0, 
           "averageRate":94.5, 
           "currencyCode":"USD", 
           "nightlyRateTotal":94.5, 
           "surchargeTotal":9.45, 
           "total":103.95 
          } 
         } 
        } 
        } 
       } 
      } 
     ] 
     } 
    } 
} 

我想數據壓扁成以下格式

{ 
    "customerSessionId":"0AB29024-F6D4-3915-0862-DB3FD1904C5A", 
    "numberOfRoomsRequested":1, 
    "moreResultsAvailable":"true", 
    "cacheKey":"-705f6d43:15086db3fd1:-4c58", 
    "cacheLocation":"10.178.144.36:7300", 
    "size":2, 
    "activePropertyCount":2, 
    "hotelId":132684, 
    "city":"Seattle", 
    "highRate":159.0, 
    "lowRate":159.0, 
    "rateCurrencyCode":"USD", 
    "roomTypeCode":10351, 
    "rateCode":10351, 
    "roomDescription":"Standard Room, 1 Queen Bed", 
    "promo":"false", 
    "averageBaseRate":159.0, 
    "averageRate":159.0, 
    "currencyCode":"USD", 
    "nightlyRateTotal":159.0, 
    "surchargeTotal":26.81, 
    "total":185.81 
} 


{ 
    "customerSessionId":"0AB29024-F6D4-3915-0862-DB3FD1904C5A", 
    "numberOfRoomsRequested":1, 
    "moreResultsAvailable":"true", 
    "cacheKey":"-705f6d43:15086db3fd1:-4c58", 
    "cacheLocation":"10.178.144.36:7300", 
    "size":2, 
    "activePropertyCount":2, 
    "hotelId":263664, 
    "city":"Las Vegas", 
    "highRate":135.0, 
    "lowRate":94.5, 
    "rateCurrencyCode":"USD", 
    "roomTypeCode":373685, 
    "rateCode":1238953, 
    "roomDescription":"Standard Room, 1 King Bed", 
    "promo":"true", 
    "averageBaseRate":135.0, 
    "averageRate":94.5, 
    "currencyCode":"USD", 
    "nightlyRateTotal":94.5, 
    "surchargeTotal":9.45, 
    "total":103.95 
} 

我一直在使用flattenDict類嘗試。不知何故,我沒有得到所需格式的輸出。以下是我的代碼

def flattenDict(d, result=None): 
    if result is None: 
     result = {} 
    for key in d: 
     value = d[key] 
     if isinstance(value, dict): 
      value1 = {} 
      for keyIn in value: 
       value1[".".join([key,keyIn])]=value[keyIn] 
      flattenDict(value1, result) 
     elif isinstance(value, (list, tuple)): 
      for indexB, element in enumerate(value): 
       if isinstance(element, dict): 
        value1 = {} 
        index = 0 
        for keyIn in element: 
         newkey = ".".join([key,keyIn])   
         value1[".".join([key,keyIn])]=value[indexB][keyIn] 
         index += 1 
        for keyA in value1: 
         flattenDict(value1, result) 
     else: 
      result[key]=value 
    return result