我正在玩Python中的一些Ticketmaster JSON数据,目的是最终使用pymongo将它发送到Mongodb集合(但还没有)。
我正在尝试使用Python检查现有字段并返回它(如果存在)。对象在对象的数组中,所以我有点绊倒...
JSON数据来自他们的活动列表。以下是一个条目。
{"_embedded":{"events":[{"name":"Ottawa Senators vs. Detroit Red Wings","type":"event","id":"17G8v3G62TrZN43","test":false,"url":"http://www.ticketmaster.ca/event/310052FFDE023552","locale":"en-us","images":[{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_TABLET_LANDSCAPE_3_2.jpg","width":1024,"height":683,"fallback":false},{"ratio":"4_3","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_CUSTOM.jpg","width":305,"height":225,"fallback":false},{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_ARTIST_PAGE_3_2.jpg","width":305,"height":203,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_EVENT_DETAIL_PAGE_16_9.jpg","width":205,"height":115,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RETINA_LANDSCAPE_16_9.jpg","width":1136,"height":639,"fallback":false},{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RETINA_PORTRAIT_3_2.jpg","width":640,"height":427,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_TABLET_LANDSCAPE_LARGE_16_9.jpg","width":2048,"height":1152,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RETINA_PORTRAIT_16_9.jpg","width":640,"height":360,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RECOMENDATION_16_9.jpg","width":100,"height":56,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_TABLET_LANDSCAPE_16_9.jpg","width":1024,"height":576,"fallback":false}],"sales":{"public":{"startDateTime":"2017-09-07T14:00:00Z","startTBD":false,"endDateTime":"2017-11-02T23:30:00Z"},"presales":[{"startDateTime":"2017-09-06T14:00:00Z","endDateTime":"2017-10-02T02:00:00Z","name":"Sens Insiders Presale"},{"startDateTime":"2017-09-06T18:00:00Z","endDateTime":"2017-10-02T02:00:00Z","name":"Sens Insiders Presale"},{"startDateTime":"2017-09-15T14:00:00Z","endDateTime":"2017-10-05T23:00:00Z","name":"Special Offer"},{"startDateTime":"2017-09-21T14:00:00Z","endDateTime":"2017-10-05T21:00:00Z","name":"Special Offer"}]},"dates":{"start":{"localDate":"2017-11-02","localTime":"19:30:00","dateTime":"2017-11-02T23:30:00Z","dateTBD":false,"dateTBA":false,"timeTBA":false,"noSpecificTime":false},"timezone":"America/New_York","status":{"code":"onsale"},"spanMultipleDays":false},"classifications":[{"primary":true,"segment":{"id":"KZFzniwnSyZfZ7v7nE","name":"Sports"},"genre":{"id":"KnvZfZ7vAdI","name":"Hockey"},"subGenre":{"id":"KZazBEonSMnZfZ7vFEE","name":"NHL"},"type":{"id":"KZAyXgnZfZ7v7l1","name":"Group"},"subType":{"id":"KZFzBErXgnZfZ7vA7d","name":"Team"}}],"promoter":{"id":"690","name":"NHL REGULAR SEASON","description":"NHL REGULAR SEASON / NTL / USA"},"promoters":[{"id":"690","name":"NHL REGULAR SEASON","description":"NHL REGULAR SEASON / NTL / USA"}],"priceRanges":[{"type":"standard","currency":"CAD","min":25.61,"max":200.61}],"products":[{"id":"17G8v3G62XN13TP","url":"http://www.ticketmaster.ca/event/310052FFA3292FE5","type":"Parking","name":"Parking: Ottawa Senators v Detroit Red Wings"}],"seatmap":{"staticUrl":"https://s1.ticketm.net/tmimages/venue/maps/que/80033s.gif"},"accessibility":{"info":"Accessible seating is available for purchase to games and events at Canadian Tire Centre. If you require accessible seats, please purchase tickets in advance that are indicated to be in accessible areas. Accessible seats may be purchased as a single or in pairs (up to a maximum of four) to accommodate both the person(s) in need of accessible seating and support person(s). Please note, for events where ticket sales are limited to fewer than four tickets per person, the same ticket restrictions will apply to the purchase of accessible seating. \r\n \r\nFor events where accessible seating is offered on the floor, those purchasing accessible seats should be aware that seats are not on a riser, so views may be obstructed.\r\n\r\nAccessible parking is available in Lot 3 (on the south side of the building). In order to access accessible parking, fans must present a valid government issued Accessible Parking Permit."},"_links":{"self":{"href":"/discovery/v2/events/17G8v3G62TrZN43?locale=en-us"},"attractions":[{"href":"/discovery/v2/attractions/K8vZ9171oB0?locale=en-us"},{"href":"/discovery/v2/attractions/K8vZ9171o20?locale=en-us"}],"venues":[{"href":"/discovery/v2/venues/KovZpZA7dnJA?locale=en-us"}]},"_embedded":{"venues":[{"name":"Canadian Tire Centre","type":"venue","id":"KovZpZA7dnJA","test":false,"url":"http://www.ticketmaster.ca/venue/401524","locale":"en-us","images":[{"ratio":"16_9","url":"https://s1.ticketm.net/dbimages/15409v.jpg","width":205,"height":115,"fallback":false}],"postalCode":"K2V 1A5","timezone":"America/New_York","city":{"name":"Kanata"},"state":{"name":"Ontario","stateCode":"ON"},"country":{"name":"Canada","countryCode":"CA"},"address":{"line1":"1000 Palladium Drive"},"location":{"longitude":"-75.9271956","latitude":"45.2965202"},"markets":[{"id":"103"}],"dmas":[{"id":519}],"social":{"twitter":{"handle":"@CdnTireCtr"}},"boxOfficeInfo":{"phoneNumberDetail":"To purchase tickets, please call 1-877-788-FANS (3267) Canadian Tire Centre 613-599-0100 Ottawa Senators Hockey Club 613-599-0250 Guest Services (During events & Sens games only) 613-599-0100 Group Sales 613-599-0210","openHoursDetail":"When an on-sale is scheduled on a day outside of our regular hours of operation the box office is open from 10am to 4pm. When an event occurs on a day outside of our regular hours of operation, the box office is open by 12:00 noon. Summer Hours - May 1 – Labour Day Monday - Friday 10 AM to 4 PM Saturday CLOSED Sunday CLOSED Winter Hours - Labour Day – April 30 Monday - Friday 10 AM to 4 PM Saturday 10 AM to 4 PM Sunday CLOSED","acceptedPaymentDetail":"Cash, Visa, AMX, MC, Debit","willCallDetail":"When picking up tickets on the day of the event, please arrive at least 1 hour prior to avoid line ups. Additional details coming soon."},"parkingDetail":"The fastest and easiest method of paying for parking is to pre-purchase you parking through Ticketmaster. As general parking is available on a first come first served basis, it is important to remember that Lots 5 and 6 will fill prior to an event start, but Lot 9 will not. Lot 9 can be accessed via Cyclone Taylor Blvd. from either Huntmar Rd. or Palladium Dr. LOT 5: Arriving Coming from the east take the Palladium Drive exit from Highway 417. Exit southbound (left). Follow the interchange, staying in the right-hand lane, and proceed straight through the traffic lights at Huntmar Rd. Once you have passed the Lot 4 entrance, merge into the right-hand lane. The Lot 5 entrance will be on your right-hand side at the Palladium Dr./Cyclone Taylor intersection. The lot is full approximately 30 minutes prior to the event start. Coming from the west take the Palladium Drive exit from Highway 417. Exit southbound (right). Follow the interchange, staying in the right-hand lane, and proceed straight through the traffic lights at Huntmar Drive. Once you have passed the Lot 4 entrance, merge into the right-hand lane. The Lot 5 entrance will be on your right-hand side at the Palladium Dr./Cyclone Taylor intersection. The lot is full approximately 30 minutes prior to the event start. Departing : One lane of traffic exits right onto Palladium Drive at the eastern corner of Lot #5. Proceed eastbound on Palladium Drive to Terry Fox Rd. For Hwy 417 access (eastbound or westbound), turn south (left) from Palladium Drive onto Terry Fox Rd. 417 East and West access through parking Lot 4. Lots 4 and 5 are divided during the event. When leaving the division is removed approximately 10 minutes after the game ends. Customers can then use NEW lot 4 exit for access to 417 East and West.","accessibleSeatingDetail":"The Canadian Tire Centre is fully accessible. All accessible seating is available online or through your local Ticketmaster Charge-by-Phone or Ticket Centre. Wheelchair patrons may also go directly to the Box Office to purchase their tickets.","generalInfo":{"generalRule":"SMOKING POLICY Canadian Tire Centre is a NON SMOKING facility. As part of the new re-entry policy, those attending events will be able to smoke outside of the venue before and after games and events only. Patrons will not be able to leave the venue to smoke and then return to the game or event. BAG POLICY: The Senators bag policy, instituted during the 2014-15 hockey season, will remain in place for the coming year. Patterned after the successful implementation of a similar policy in the National Football League and the NHL’s new mandate for event operations and recommendations for enhanced security, the policy will only allow patrons to bring small soft-sided handbags, diaper bags and bags specifically required for medical purposes into hockey games, shows and events. Only bags fitting the above approved criteria will be accepted into the venue. This indicates specifically that backpacks, coolers and duffle bags will not be permitted. Canadian Tire Centre wants to make this reminder specifically to fans that arrive without a vehicle so they can plan accordingly. IN/OUT Policy: Canadian Tire Centre enforces a strict no re-admission policy. Any guests exiting the facility will not be allowed to re-enter the building. PROHIBITED ITEMS: Food or beverages purchased outside Canadian Tire Centre Bottles and cans Coolers Sticks/flagpoles Aerosol cans Fireworks Skateboards Inline skates Roller skates Illegal drugs or noxious substances Cameras, sound and/or video recording devices as per event restrictions Helium balloons Stolen tickets Items prohibited based on promoter request Horns or noisemakers Frisbees and/or beach balls Laser pointers Any mechanically enhanced noise making devices, specifically air horns with sirens, whistles Spiked or studded jewelry or belts Weapons and dangerous devices of any type such as firearms, explosives, stun guns, handcuffs, clubs, batons, martial arts instruments, knives, etc. Building management reserves the right in their absolute discretion to deny access for any item(s) deemed a safety concern or may result in an undesirable impact on the guest experience. Illegal items will be turned over to Ottawa Police. If a guest is found in the facility with a prohibited item, the item will be confiscated, and, in some cases, the fan may be evicted from the building. Can I bring a sign / banner / flag to a Senators Game? Yes but there are restrictions. Banners, flags and signs are not permitted in the Canadian Tire Centre for non-Senators events. For Senators games, guests are permitted to bring banners into the arena provided they do not interfere with the game and other fans’ enjoyment of the game. Banners can neither cover any advertising or building signs, nor can they compete with existing building signs and advertising and they must not be attached to the building in any manner. They also cannot carry disparaging or obscene messages. Signs greater than 24? (61cm) x 24? (61cm) in size must be able to roll up or fold. Poles in excess of 36 inches (91cm) used to display banners or flags are not permitted.","childRule":"Most Canadian Tire Centre events do not require a child who is less than two-years-old to have a ticket unless they are to occupy a seat. This policy will vary by event, so please inquire at time of ticket purchase."},"upcomingEvents":{"_total":55,"tmr":44,"ticketmaster":11},"_links":{"self":{"href":"/discovery/v2/venues/KovZpZA7dnJA?locale=en-us"}}}],"attractions":[{"name":"Ottawa Senators","type":"attraction","id":"K8vZ9171oB0","test":false,"url":"http://www.ticketmaster.ca/artist/805997","locale":"en-us","images":[{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_TABLET_LANDSCAPE_3_2.jpg","width":1024,"height":683,"fallback":false},{"ratio":"4_3","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_CUSTOM.jpg","width":305,"height":225,"fallback":false},{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_ARTIST_PAGE_3_2.jpg","width":305,"height":203,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_EVENT_DETAIL_PAGE_16_9.jpg","width":205,"height":115,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RETINA_LANDSCAPE_16_9.jpg","width":1136,"height":639,"fallback":false},{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RETINA_PORTRAIT_3_2.jpg","width":640,"height":427,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_TABLET_LANDSCAPE_LARGE_16_9.jpg","width":2048,"height":1152,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RETINA_PORTRAIT_16_9.jpg","width":640,"height":360,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_RECOMENDATION_16_9.jpg","width":100,"height":56,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/f33/40b2fec9-c258-4c43-b811-437d9b1cdf33_490471_TABLET_LANDSCAPE_16_9.jpg","width":1024,"height":576,"fallback":false}],"classifications":[{"primary":true,"segment":{"id":"KZFzniwnSyZfZ7v7nE","name":"Sports"},"genre":{"id":"KnvZfZ7vAdI","name":"Hockey"},"subGenre":{"id":"KZazBEonSMnZfZ7vFEE","name":"NHL"},"type":{"id":"KZAyXgnZfZ7v7l1","name":"Group"},"subType":{"id":"KZFzBErXgnZfZ7vA7d","name":"Team"}}],"upcomingEvents":{"_total":89,"tmr":76,"ticketmaster":13},"_links":{"self":{"href":"/discovery/v2/attractions/K8vZ9171oB0?locale=en-us"}}},{"name":"Detroit Red Wings","type":"attraction","id":"K8vZ9171o20","test":false,"url":"http://www.ticketmaster.ca/artist/805938","locale":"en-us","images":[{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_EVENT_DETAIL_PAGE_16_9.jpg","width":205,"height":115,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_RETINA_PORTRAIT_16_9.jpg","width":640,"height":360,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_TABLET_LANDSCAPE_16_9.jpg","width":1024,"height":576,"fallback":false},{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_ARTIST_PAGE_3_2.jpg","width":305,"height":203,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_RECOMENDATION_16_9.jpg","width":100,"height":56,"fallback":false},{"ratio":"4_3","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_CUSTOM.jpg","width":305,"height":225,"fallback":false},{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_TABLET_LANDSCAPE_3_2.jpg","width":1024,"height":683,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_TABLET_LANDSCAPE_LARGE_16_9.jpg","width":2048,"height":1152,"fallback":false},{"ratio":"16_9","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_RETINA_LANDSCAPE_16_9.jpg","width":1136,"height":639,"fallback":false},{"ratio":"3_2","url":"https://s1.ticketm.net/dam/a/7c8/7ea7bf7c-90a7-4b69-a4d2-aab1f16267c8_25431_RETINA_PORTRAIT_3_2.jpg","width":640,"height":427,"fallback":false}],"classifications":[{"primary":true,"segment":{"id":"KZFzniwnSyZfZ7v7nE","name":"Sports"},"genre":{"id":"KnvZfZ7vAdI","name":"Hockey"},"subGenre":{"id":"KZazBEonSMnZfZ7vFEE","name":"NHL"},"type":{"id":"KZAyXgnZfZ7v7l1","name":"Group"},"subType":{"id":"KZFzBErXgnZfZ7vA7d","name":"Team"}}],"upcomingEvents":{"_total":90,"tmr":77,"ticketmaster":13},"_links":{"self":{"href":"/discovery/v2/attractions/K8vZ9171o20?locale=en-us"}}}]}}]},"_links":{"self":{"href":"/discovery/v2/events?countryCode=CA&stateCode=ON&id=17G8v3G62TrZN43"}},"page":{"size":20,"totalElements":1,"totalPages":1,"number":0}}
这是我的代码。 'apikey'在这里被掩盖了。我正在寻找'id'和'name'。如果每个都存在,我想返回值,如果不存在,则返回'undefined'。
import requests
import json
import sys
url='https://app.ticketmaster.com/discovery/v2/events.json'
payload = {'countryCode':'CA','stateCode':'ON','apikey':'xxx'}
r=requests.get(url,params=payload)
data=json.loads(r.text)
if 'events' in data.get('_embedded',{}):
print "OK"
events=data['_embedded']['events']
else:
print 'NO DATA'
sys.exit(1)
for ev in events:
print ''
print ''
my_event={}
my_event['eventId']=ev['id']
my_event['eventName']=ev['name']
print my_event
我已经尝试但是它不太有效。绝对缺少一些东西......
if 'id' in events:
my_event['eventId']=ev['id']
else:
my_event['eventId']='undefined'