使用Python引用的具有唯一ID的JSON feed

时间:2018-06-29 14:07:57

标签: python json

我正在使用Python解析JSON feed。 JSON提要每30秒左右更新一次。我需要从对战游戏中提取信息,但是对战游戏ID的唯一ID节点在每个游戏上都会发生变化。

我已经可以通过对uniqueID进行硬编码来解析所需的数据,但是当新游戏进入提要时,我将不知道uniqueID是什么,因此代码将失败。

以下代码确实有效,但是我需要一种查找或通配符(我知道直接在Python中无法实现)唯一ID 1572805的方法。

SystemCheckError: System check identified some issues:

ERRORS:
oauth.AccessToken.application: (fields.E304) Reverse accessor for 'AccessToken.application' clashes with reverse accessor for 'AccessToken.application'.
    HINT: Add or change a related_name argument to the definition for 'AccessToken.application' or 'AccessToken.application'.
oauth.AccessToken.source_refresh_token: (fields.E304) Reverse accessor for 'AccessToken.source_refresh_token' clashes with reverse accessor for 'AccessToken.source_refresh_token'.
    HINT: Add or change a related_name argument to the definition for 'AccessToken.source_refresh_token' or 'AccessToken.source_refresh_token'.
oauth.AccessToken.source_refresh_token: (fields.E305) Reverse query name for 'AccessToken.source_refresh_token' clashes with reverse query name for 'AccessToken.source_refresh_token'.
    HINT: Add or change a related_name argument to the definition for 'AccessToken.source_refresh_token' or 'AccessToken.source_refresh_token'.
oauth.Grant.application: (fields.E304) Reverse accessor for 'Grant.application' clashes with reverse accessor for 'Grant.application'.
    HINT: Add or change a related_name argument to the definition for 'Grant.application' or 'Grant.application'.
oauth.RefreshToken.access_token: (fields.E304) Reverse accessor for 'RefreshToken.access_token' clashes with reverse accessor for 'RefreshToken.access_token'.
    HINT: Add or change a related_name argument to the definition for 'RefreshToken.access_token' or 'RefreshToken.access_token'.
oauth.RefreshToken.access_token: (fields.E305) Reverse query name for 'RefreshToken.access_token' clashes with reverse query name for 'RefreshToken.access_token'.
    HINT: Add or change a related_name argument to the definition for 'RefreshToken.access_token' or 'RefreshToken.access_token'.
oauth.RefreshToken.application: (fields.E304) Reverse accessor for 'RefreshToken.application' clashes with reverse accessor for 'RefreshToken.application'.
    HINT: Add or change a related_name argument to the definition for 'RefreshToken.application' or 'RefreshToken.application'.
oauth2_provider.AccessToken.application: (fields.E304) Reverse accessor for 'AccessToken.application' clashes with reverse accessor for 'AccessToken.application'.
    HINT: Add or change a related_name argument to the definition for 'AccessToken.application' or 'AccessToken.application'.
oauth2_provider.AccessToken.source_refresh_token: (fields.E304) Reverse accessor for 'AccessToken.source_refresh_token' clashes with reverse accessor for 'AccessToken.source_refresh_token'.
    HINT: Add or change a related_name argument to the definition for 'AccessToken.source_refresh_token' or 'AccessToken.source_refresh_token'.
oauth2_provider.AccessToken.source_refresh_token: (fields.E305) Reverse query name for 'AccessToken.source_refresh_token' clashes with reverse query name for 'AccessToken.source_refresh_token'.
    HINT: Add or change a related_name argument to the definition for 'AccessToken.source_refresh_token' or 'AccessToken.source_refresh_token'.
oauth2_provider.Grant.application: (fields.E304) Reverse accessor for 'Grant.application' clashes with reverse accessor for 'Grant.application'.
    HINT: Add or change a related_name argument to the definition for 'Grant.application' or 'Grant.application'.
oauth2_provider.RefreshToken.access_token: (fields.E304) Reverse accessor for 'RefreshToken.access_token' clashes with reverse accessor for 'RefreshToken.access_token'.
    HINT: Add or change a related_name argument to the definition for 'RefreshToken.access_token' or 'RefreshToken.access_token'.
oauth2_provider.RefreshToken.access_token: (fields.E305) Reverse query name for 'RefreshToken.access_token' clashes with reverse query name for 'RefreshToken.access_token'.
    HINT: Add or change a related_name argument to the definition for 'RefreshToken.access_token' or 'RefreshToken.access_token'.
oauth2_provider.RefreshToken.application: (fields.E304) Reverse accessor for 'RefreshToken.application' clashes with reverse accessor for 'RefreshToken.application'.
    HINT: Add or change a related_name argument to the definition for 'RefreshToken.application' or 'RefreshToken.application'.

下面是JSON的摘录,您可以在其中看到每个游戏的数据都有一个新的uniqueID:-

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"

如何重新创建代码,以便查找所有uniqueID,然后能够使用这些预定义的uniqueID解析JSON?

谢谢。

2 个答案:

答案 0 :(得分:0)

使用以下代码来解析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']

答案 1 :(得分:0)

使用IsCompleted = 1字典并遍历所有匹配项。

Match