[英]JSON feed with unique IDs referenced using Python
I am using Python to parse a JSON feed. 我正在使用Python解析JSON feed。 The JSON feed updates every 30 seconds or so. JSON提要每30秒左右更新一次。 I need to extract the information from match games but the unique ID node for the match game ID changes on each game. 我需要从对战游戏中提取信息,但是对战游戏ID的唯一ID节点在每个游戏上都会发生变化。
I have been able to parse the data I need by hard coding the uniqueID but when new games come into the feed, I will not know what the uniqueID is and therefore the code will fail. 我已经可以通过硬编码uniqueID来解析所需的数据,但是当新游戏进入Feed时,我将不知道UniqueID是什么,因此代码将失败。
The below code does work but I need a way of looking up or wild-carding (which I know isn't possible in Python directly) the unique ID 1572805. 下面的代码确实有效,但是我需要一种查找或通配符(我知道直接在Python中是不可能的)唯一ID 1572805的方法。
import urllib2, cookielib, json, io
import datetime
url = "JSON URL - REMOVED"
sportsFeed = r"c:\feeds\results.txt"
try:
scorepro = urllib2.urlopen(url)
wjson = scorepro.read()
wjdata = json.loads(wjson)
today = str(datetime.datetime.today().strftime('%Y-%m-%d'))
playerone = wjdata['list']['Sport']['2']['Matchday'][today]['Match']['1572805']['Home']['name']
print "playerone = " + playerone
except:
print "error"
Below is an extract from the JSON where you can see each game's data is given a new uniqueID:- 以下是JSON的摘录,您可以在其中看到每个游戏的数据都有一个新的uniqueID:-
"timestamp":1530275075969,
"list":{
"Sport":{
"2":{
"id":"2",
"name":"TENNIS",
"code":"TEN",
"hid":"6268717",
"Matchday":{
"2018-06-28":{
"date":"2018-06-28",
"Match":{
"1572805":{
"ct":0,
"id":"1572805",
"lastPeriod":"Set4",
"leagueCode":"43548",
"leagueSort":0,
"leagueType":"Tournament",
"startTime":"10:05",
"status":"Fin",
"statustype":"fin",
"type":2,
"visible":"1",
"Home":{
"id":"43095",
"name":"BOLT A. (AUS)",
"serve":"1",
"seed":"",
"standing":""
},
"Away":{
"id":"43035",
"name":"KOKKINAKIS T. (AUS)",
"serve":"0",
"seed":"",
"standing":""
},
"Results":{
"1":{
"id":"1",
"name":"CURRENT",
"value":"3-1"
},
"Period":{
"1":{
"id":1,
"name":"S1",
"detail":"SCORE",
"value":"7-6",
"tiebreak":"8-6"
},
"2":{
"id":2,
"name":"S2",
"detail":"SCORE",
"value":"7-6",
"tiebreak":"7-4"
},
"3":{
"id":3,
"name":"S3",
"detail":"SCORE",
"value":"4-6",
"tiebreak":""
},
"4":{
"id":4,
"name":"S4",
"detail":"SCORE",
"value":"7-5",
"tiebreak":""
},
"5":{
"id":5,
"name":"S5",
"detail":"SCORE",
"value":"0-0",
"tiebreak":""
}
}
},
"Information":{
"season":{
"id":"364",
"name":"2018"
},
"country":{
"id":"61738",
"name":"ATP SINGLES"
},
"league":{
"id":"43548",
"name":"WIMBLEDON"
},
"shortName":"ATP-S",
"round":"QR32",
"note":"",
"bitArray":"",
"timestamp":""
}
},
"1572811":{
"ct":0,
"id":"1572811",
"lastPeriod":"Set4",
"leagueCode":"43548",
"leagueSort":0,
"leagueType":"Tournament",
"startTime":"10:05",
"status":"Fin",
"statustype":"fin",
"type":2,
"visible":"1",
"Home":{
"id":"16910",
"name":"ROBERT S. (FRA)",
"serve":"0",
"seed":"",
"standing":""
},
"Away":{
"id":"47867",
"name":"VANNI L. (ITA)",
"serve":"1",
"seed":"",
"standing":""
},
"Results":{
"1":{
"id":"1",
"name":"CURRENT",
"value":"3-1"
},
"Period":{
"1":{
"id":1,
"name":"S1",
"detail":"SCORE",
"value":"6-4",
"tiebreak":""
},
"2":{
"id":2,
"name":"S2",
"detail":"SCORE",
"value":"3-6",
"tiebreak":""
},
"3":{
"id":3,
"name":"S3",
"detail":"SCORE",
"value":"7-5",
"tiebreak":""
},
"4":{
"id":4,
"name":"S4",
"detail":"SCORE",
"value":"6-1",
"tiebreak":""
},
"5":{
"id":5,
"name":"S5",
"detail":"SCORE",
"value":"0-0",
"tiebreak":""
}
}
},
"Information":{
"season":{
"id":"364",
"name":"2018"
},
"country":{
"id":"61738",
"name":"ATP SINGLES"
},
"league":{
"id":"43548",
"name":"WIMBLEDON"
},
"shortName":"ATP-S",
"round":"QR32",
"note":"",
"bitArray":"",
"timestamp":""
}
},
"1572826":{
"ct":0,
"id":"1572826",
"lastPeriod":"Set4",
"leagueCode":"43548",
"leagueSort":0,
"leagueType":"Tournament",
"startTime":"10:05",
"status":"Fin",
"statustype":"fin",
"type":2,
"visible":"1",
"Home":{
"id":"44005",
"name":"WARD J. (GBR)",
"serve":"1",
"seed":"",
"standing":""
},
"Away":{
"id":"55639",
"name":"BONZI B. (FRA)",
"serve":"0",
"seed":"",
"standing":""
},
"Results":{
"1":{
"id":"1",
"name":"CURRENT",
"value":"1-3"
},
"Period":{
"1":{
"id":1,
"name":"S1",
"detail":"SCORE",
"value":"4-6",
"tiebreak":""
},
"2":{
"id":2,
"name":"S2",
"detail":"SCORE",
"value":"2-6",
"tiebreak":""
},
"3":{
"id":3,
"name":"S3",
"detail":"SCORE",
"value":"6-1",
"tiebreak":""
},
"4":{
"id":4,
"name":"S4",
"detail":"SCORE",
"value":"0-6",
"tiebreak":""
},
"5":{
"id":5,
"name":"S5",
"detail":"SCORE",
"value":"0-0",
"tiebreak":""
}
}
},
"Information":{
"season":{
"id":"364",
"name":"2018"
},
"country":{
"id":"61738",
"name":"ATP SINGLES"
},
"league":{
"id":"43548",
"name":"WIMBLEDON"
},
"shortName":"ATP-S",
"round":"QR32",
"note":"",
"bitArray":"",
"timestamp":""
}
},
How do I re-create my code so I can lookup all uniqueIDs and then be able to parse the JSON with these pre-defined uniqueIDs? 如何重新创建代码,以便可以查找所有uniqueID,然后能够使用这些预定义的uniqueID解析JSON?
Thank you. 谢谢。
Use the following code to parse the ID from the JSON incrementing the key # by one each time, for example:- 使用以下代码来解析JSON中的ID,每次将键#递增1,例如:
id = wjdata['list']['Sport']['2']['Matchday'][today]['Match'].keys()[0]
playerone = wjdata['list']['Sport']['2']['Matchday'][today]['Match'][id]['Home']['name']
id = wjdata['list']['Sport']['2']['Matchday'][today]['Match'].keys()[1]
playerone = wjdata['list']['Sport']['2']['Matchday'][today]['Match'][id]['Home']['name']
Using Match
dict and looping over all matches. 使用Match
dict并遍历所有匹配项。
import urllib2, cookielib, json, io
import datetime
url = "JSON URL - REMOVED"
sportsFeed = r"c:\feeds\results.txt"
try:
scorepro = urllib2.urlopen(url)
wjson = scorepro.read()
wjdata = json.loads(wjson)
today = str(datetime.datetime.today().strftime('%Y-%m-%d'))
matches = wjdata['list']['Sport']['2']['Matchday'][today]['Match']
for id, match in matches.items():
player_name = match['Home']['name']
print "player = " + player_name
except:
print "error"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.