2016-03-22 98 views
0
data = response.json() 
sortJson = json.dumps(data, sort_keys=True, 
       indent=2, separators=(',', ':')) 

result = json.loads (data) 
print ('"saleTotal":', result['trips']['tripOption']['pricing']['saleTotal']) 

這是我目前的代碼。我期待通過嵌套的JSON文件來分析,但每次我跑這個時候,我得到以下錯誤:通過嵌套的JSON Python解析

類型錯誤:JSON對象必須str的,而不是「字典」

JSON文件當漂亮的印花原來是這樣的:

{ 
    "kind":"qpxExpress#tripsSearch", 
    "trips":{ 
    "data":{ 
     "aircraft":[ 
     { 
      "code":"321", 
      "kind":"qpxexpress#aircraftData", 
      "name":"Airbus A321" 
     } 
     ], 
     "airport":[ 
     { 
      "city":"ORL", 
      "code":"MCO", 
      "kind":"qpxexpress#airportData", 
      "name":"Orlando International" 
     }, 
     { 
      "city":"CHI", 
      "code":"ORD", 
      "kind":"qpxexpress#airportData", 
      "name":"Chicago O'Hare" 
     } 
     ], 
     "carrier":[ 
     { 
      "code":"F9", 
      "kind":"qpxexpress#carrierData", 
      "name":"Frontier Airlines, Inc." 
     } 
     ], 
     "city":[ 
     { 
      "code":"CHI", 
      "kind":"qpxexpress#cityData", 
      "name":"Chicago" 
     }, 
     { 
      "code":"ORL", 
      "kind":"qpxexpress#cityData", 
      "name":"Orlando" 
     } 
     ], 
     "kind":"qpxexpress#data", 
     "tax":[ 
     { 
      "id":"ZP", 
      "kind":"qpxexpress#taxData", 
      "name":"US Flight Segment Tax" 
     }, 
     { 
      "id":"AY_001", 
      "kind":"qpxexpress#taxData", 
      "name":"US September 11th Security Fee" 
     }, 
     { 
      "id":"US_001", 
      "kind":"qpxexpress#taxData", 
      "name":"US Transportation Tax" 
     }, 
     { 
      "id":"XF", 
      "kind":"qpxexpress#taxData", 
      "name":"US Passenger Facility Charge" 
     } 
     ] 
    }, 
    "kind":"qpxexpress#tripOptions", 
    "requestId":"2z1TQ9iVMcSlUH8HW0O0eq", 
    "tripOption":[ 
     { 
     "id":"WQZ8ICu2L8RLqt1MyMNFAQ001", 
     "kind":"qpxexpress#tripOption", 
     "pricing":[ 
      { 
      "baseFareTotal":"USD37.11", 
      "fare":[ 
       { 
       "basisCode":"Z00ZSS5", 
       "carrier":"F9", 
       "destination":"ORL", 
       "id":"AR5um4n2cToXHml3a125O0CU7toTISvPQER/01Xhbf2E", 
       "kind":"qpxexpress#fareInfo", 
       "origin":"CHI" 
       } 
      ], 
      "fareCalculation":"ORD F9 MCO Q9.29 Q4.65 23.17Z00ZSS5 USD 37.11 END ZP ORD XT 2.79US 4.00ZP 5.60AY 4.50XF ORD4.50", 
      "kind":"qpxexpress#pricingInfo", 
      "latestTicketingTime":"2016-03-22T00:24-04:00", 
      "passengers":{ 
       "adultCount":1, 
       "kind":"qpxexpress#passengerCounts" 
      }, 
      "ptc":"ADT", 
      "saleFareTotal":"USD37.11", 
      "saleTaxTotal":"USD16.89", 
      "saleTotal":"USD54.00", 
      "segmentPricing":[ 
       { 
       "fareId":"AR5um4n2cToXHml3a125O0CU7toTISvPQER/01Xhbf2E", 
       "kind":"qpxexpress#segmentPricing", 
       "segmentId":"GoIDkawPBE2TZk14" 
       } 
      ], 
      "tax":[ 
       { 
       "chargeType":"GOVERNMENT", 
       "code":"US", 
       "country":"US", 
       "id":"US_001", 
       "kind":"qpxexpress#taxInfo", 
       "salePrice":"USD2.79" 
       }, 
       { 
       "chargeType":"GOVERNMENT", 
       "code":"AY", 
       "country":"US", 
       "id":"AY_001", 
       "kind":"qpxexpress#taxInfo", 
       "salePrice":"USD5.60" 
       }, 
       { 
       "chargeType":"GOVERNMENT", 
       "code":"XF", 
       "country":"US", 
       "id":"XF", 
       "kind":"qpxexpress#taxInfo", 
       "salePrice":"USD4.50" 
       }, 
       { 
       "chargeType":"GOVERNMENT", 
       "code":"ZP", 
       "country":"US", 
       "id":"ZP", 
       "kind":"qpxexpress#taxInfo", 
       "salePrice":"USD4.00" 
       } 
      ] 
      } 
     ], 
     "saleTotal":"USD54.00", 
     "slice":[ 
      { 
      "duration":167, 
      "kind":"qpxexpress#sliceInfo", 
      "segment":[ 
       { 
       "bookingCode":"Z", 
       "bookingCodeCount":9, 
       "cabin":"COACH", 
       "duration":167, 
       "flight":{ 
        "carrier":"F9", 
        "number":"1294" 
       }, 
       "id":"GoIDkawPBE2TZk14", 
       "kind":"qpxexpress#segmentInfo", 
       "leg":[ 
        { 
        "aircraft":"321", 
        "arrivalTime":"2016-05-11T09:42-04:00", 
        "departureTime":"2016-05-11T05:55-05:00", 
        "destination":"MCO", 
        "duration":167, 
        "id":"LQKIza3yQIpaLyDq", 
        "kind":"qpxexpress#legInfo", 
        "meal":"Food and Beverages for Purchase", 
        "mileage":1006, 
        "origin":"ORD", 
        "originTerminal":"3", 
        "secure":true 
        } 
       ], 
       "marriedSegmentGroup":"0" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
} 

最後我試圖找到saleTotal和印有沿着它相應的值: saleTotal:54.00

+1

哪一行,你得到'TypeError'上? –

+0

'result = json.loads(data)'是我在 – nill

+0

上得到TypeError的那句好的。這是因爲'json.loads' [需要你傳遞一個str](https://docs.python.org/3/library/json.html#json.loads)。是否有一個原因,你正在做'json.dumps()'?如果它已經是'dict',你可能很容易獲得'saleTotal'。 –

回答

1

tripOptionpricing是對象的名單,如果你考慮到這一點,將工作:

print ('"saleTotal":', result['trips']['tripOption'][0]['pricing'][0]['saleTotal']) 

輸出:

"saleTotal": USD54.00 
+0

更改了代碼的這一部分後,我仍然在'result = json.loads(data)'行上得到'TypeError'。 – nill

+0

對於我來說,當我將它分配給一個變量並調用'json.loads'時,JSON加載就好了。如果你的代碼中的數據不是'str',而是'dict',那麼沒有理由像其他人一樣調用json.loads。 – niemmi

+0

我所要做的就是刪除'result = json.loads(data)',就像之前所說的@ gr1zzly be4r。感謝你們兩位的幫助 – nill