简体   繁体   中英

Parsing json value in Python

I am receiving a json from a web page request.

    {
  "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
}

i am having problems getting the "Items" child. So far i did this:

price_item_list = cruise_price_data["Items"]

and if i print price_item_list i get this:

    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

I am thinking this must be some conversion mistake. Where is my mistake?

You need first to convert your json data to a Python object like below:

import json

data = json.loads(your_json)

Now, you should be able to iterate over data :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM