在json中访问随机命名键

时间:2016-03-16 12:15:56

标签: python json

尝试访问具有随机键名称的数组。因为我不能像这样访问密钥,因为它们总是不同(因为它们是日期)并且它们不遵循任何模式:

for d in data:
    for y in d['nightlyDetails']['randomkeyname']:
        print(y)

如何在这个json上访问它?

"checkOut": "2016-02-04",
  "sourceBusinessName": "xxxx.com",
  "folioStatusIdUserDef": "0",
  "discounts": 0,
  "reservationDate": "2015-06-30 07:13:11",
  "additionalGuestIds": [],
  "lastNightAuditDate": "0000-00-00",
  "adjustmentTotal": 0,
  "totalRent": 1280,
  "noOfSplits": 1,
  "checkoutDate": "2016-02-04 10:19:30",
  "noOfNights": 1,
  "folioNo": "44108",
  "pm_code": "",
  "arrivalAmpm": "am",
  "folioStatus": "Checked-Out",
  "checkinBy": "xxxx",
  "reservedBy": "Oboe Reservation",
  "event_training_id": "0",
  "housekeepingRuleId": "0",
  "occupancyAdults": "2",
  "nightlyDetails": {
    "2016-02-03": {
      "accountLabelId": 1,
      "accountCategoryId": 6,
      "NightlyOverrideType": "0",
      "accountCategoryType": "Add-on",
      "NightlyRoomTransferId": "0",
      "accountLabelType": " Rooms \u74e6\u5382\u623f\u95f4",
      "NightlyRateId": "196",
      "NightlyActualPrice": "1280.000000",
      "NightlydiscountType": "0",
      "NightlyOverride": "0.000000",
      "rateName": "SQ",
      "NightAuditDate": "0000-00-00",
      "NightlyPaidByGroup": "0",
      "NightlyOfferedPrice": "1280.000000",
      "Nightlydiscount": "0.00",
      "NightlyActualDiscountPrice": 1280
    }

1 个答案:

答案 0 :(得分:2)

您可以通过d.keys()调用d.items()或键的元组和值来访问任何字典的键。

for d in data:
    for (date, details) in d['nightlyDetails'].items():
        for (key, value) in details.items():
            print(date, key, value)