簡體   English   中英

在 Python 中解析 json 值

[英]Parsing json value in 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

我想這一定是一些轉換錯誤。 我的錯誤在哪里?

您首先需要將 json 數據轉換為 Python 對象,如下所示:

import json

data = json.loads(your_json)

現在,您應該能夠遍歷data

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM