繁体   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