2016-12-04 1 views
-1

웹 페이지 요청에서 json을 수신하고 있습니다.Python의 json 값 구문 분석

{ 
    "ChildAvailability": { 
    "ChildrenStatus": "Available", 
    "ChildrenTitle": "4 years to 18 years", 
    "MinimumAgeErrorMessage": "Due to safety issues we do not allow any baby under the age of 1 year old at the time of sailing. For this cruise, this means any baby born on or after 05 Dec 2015. Inappropriately adding guests, could result in your booking being cancelled.", 
    "InfantAvailability": 14, 
    "InfantBirthdayRange": { 
     "Item1": "2013-12-06", 
     "Item2": "2015-12-05" 
    }, 
    "ChildrenBirthdayRange": { 
     "Item1": "1998-12-06", 
     "Item2": "2013-12-05" 
    }, 
    "InfantStatus": "Limited", 
    "InfantTitle": "12 months to 3 years", 
    "OverallAvailability": 156, 
    "VoyageCode": "P652" 
    }, 
    "EligibleChildFareThreshold": "2003-12-05", 
    "Items": [ 
    { 
     "Fare": { 
     "AdditionalPromoCodes": [], 
     "CabinSize": null, 
     "DisplayWasPrice": false, 
     "GradeCode": "", 
     "IsDiscreet": false, 
     "IsObstructed": false, 
     "IsPremium": false, 
     "NumberOfPax": 0, 
     "Price": -1.0, 
     "PricePerPerson": -1.0, 
     "PricePerPersonPerNight": -1.0, 
     "PriceWas": -1.0, 
     "PromoCode": "", 
     "RoomTypeCode": null, 
     "Usps": [] 
     }, 
     "HasAvailability": false, 
     "CanHaveObstructed": false, 
     "CanHavePremium": false, 
     "RoomTypeCode": "I" 
    }, 
    { 
     "CanHaveObstructed": true, 
     "CanHavePremium": false, 
     "Fare": { 
     "AdditionalPromoCodes": null, 
     "CabinSize": "TWIN", 
     "DisplayWasPrice": true, 
     "GradeCode": "OV", 
     "IsDiscreet": false, 
     "IsObstructed": true, 
     "IsPremium": false, 
     "NumberOfPax": 2, 
     "Price": 1398.000000, 
     "PricePerPerson": 699.000000, 
     "PricePerPersonPerNight": 87.0, 
     "PriceWas": 2908.000000, 
     "PromoCode": "FL9", 
     "RoomTypeCode": "O", 
     "Usps": [] 
     }, 
     "HasAvailability": true, 
     "RoomTypeCode": "O" 
    }, 
    { 
     "Fare": { 
     "AdditionalPromoCodes": [], 
     "CabinSize": null, 
     "DisplayWasPrice": false, 
     "GradeCode": "", 
     "IsDiscreet": false, 
     "IsObstructed": false, 
     "IsPremium": false, 
     "NumberOfPax": 0, 
     "Price": -1.0, 
     "PricePerPerson": -1.0, 
     "PricePerPersonPerNight": -1.0, 
     "PriceWas": -1.0, 
     "PromoCode": "", 
     "RoomTypeCode": null, 
     "Usps": [] 
     }, 
     "HasAvailability": false, 
     "CanHaveObstructed": false, 
     "CanHavePremium": false, 
     "RoomTypeCode": "B" 
    }, 
    { 
     "Fare": { 
     "AdditionalPromoCodes": [], 
     "CabinSize": null, 
     "DisplayWasPrice": false, 
     "GradeCode": "", 
     "IsDiscreet": false, 
     "IsObstructed": false, 
     "IsPremium": false, 
     "NumberOfPax": 0, 
     "Price": -1.0, 
     "PricePerPerson": -1.0, 
     "PricePerPersonPerNight": -1.0, 
     "PriceWas": -1.0, 
     "PromoCode": "", 
     "RoomTypeCode": null, 
     "Usps": [] 
     }, 
     "HasAvailability": false, 
     "CanHaveObstructed": false, 
     "CanHavePremium": false, 
     "RoomTypeCode": "M" 
    } 
    ], 
    "QuoteId": null 
} 

"항목"하위 항목을 가져 오는 데 문제가 있습니다. 지금까지 나는이 한 :

price_item_list = cruise_price_data["Items"] 

을하고 난 price_item_list를 인쇄하면 난이 얻을 :이 일부 변환 실수 있어야합니다 생각하고

Traceback (most recent call last): 
    File "/home/fixxxer/PycharmProjects/POCruses/main.py", line 94, in <module> 
    print(price_item_list["RoomTypeCode"]) 
TypeError: list indices must be integers or slices, not str 

합니다. 내 실수는 어디 갔지? 당신이 data을 반복 할 수 있어야한다, 지금

import json 

data = json.loads(your_json) 

:

+2

은' price_item_list'을 쓰면, 할 수없는'price_item_list [ "RoomTypeCode"]'이 출력됩니다. 사전이 아닙니다. –

+2

'price_item_list [0] [ 'Fare'] [ 'RoomTypeCode']', 즉 해당 목록에서 ** 첫 번째 항목 **의 코드를 사용해보십시오. – jonrsharpe

+0

'Items'는 목록입니다. 당신은에 그것을 같이 처리해야합니다. 당신이 그것을 반복하거나 그냥 하나의 아이템을 얻습니다 :'cruise_price_data [ "Items [] [0]' –

답변

1

당신은 다음과 같은 파이썬 객체로 JSON 데이터를 변환 먼저 필요 당신은 인쇄되지 않습니다

for item in data['Items']: 
    print(item['RoomTypeCode']) 
    # ... 
+0

당신은'Fare'를 놓쳤습니다 –

+1

@ cricket_007'RoomTypeCode'는'Fare'와 같은 레벨입니다 – ettanany

+0

오. 두 가지가 있습니다 ... 혼란 스럽습니다. –