웹 페이지 요청에서 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)
:
은' price_item_list'을 쓰면, 할 수없는'price_item_list [ "RoomTypeCode"]'이 출력됩니다. 사전이 아닙니다. –
'price_item_list [0] [ 'Fare'] [ 'RoomTypeCode']', 즉 해당 목록에서 ** 첫 번째 항목 **의 코드를 사용해보십시오. – jonrsharpe
'Items'는 목록입니다. 당신은에 그것을 같이 처리해야합니다. 당신이 그것을 반복하거나 그냥 하나의 아이템을 얻습니다 :'cruise_price_data [ "Items [] [0]' –