简体   繁体   中英

JSON feed with unique IDs referenced using Python

I am using Python to parse a JSON feed. The JSON feed updates every 30 seconds or so. I need to extract the information from match games but the unique ID node for the match game ID changes on each game.

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.

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.

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:-

"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?

Thank you.

Use the following code to parse the ID from the JSON incrementing the key # by one each time, for example:-

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.

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"

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