这是我第一次使用JSON,我被挂在这里。你知道吗
我想用Python中的json.loads(tackles)
解析这个名为tackles
的JSON字符串,得到了错误TypeError: expected string or buffer
在此之后,我使用了json.loads(str(tackles))
,但是失败了,出现了以下错误ValueError: Expecting property name: line 1 column 2 (char 1)
那么我到底需要做些什么才能让它工作呢?
最好的例子是显示如何在团队名称旁边打印所有tackleWontotal
值。你知道吗
以下是JSON字符串:
"{
u'statColumns': [
u'apps',
u'subOn',
u'minsPlayed',
u'tackleWonTotal',
u'challengeLost',
u'tackleTotalAttempted'
],
u'paging': {
u'firstRecordIndex': 0,
u'resultsPerPage': 0,
u'lastRecordIndex': 0,
u'totalPages': 0,
u'currentPage': 1,
u'totalResults': 0
},
u'teamTableStats': [ {
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 10.75,
u'minsPlayed': 360,
u'name': u'ManCity',
u'ranking': 1,
u'rating': 7.297500000000002,
u'apps': 4,
u'tackleWonTotal': 14.75,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'ManchesterCity',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 167,
u'tournamentId': 2,
u'tackleTotalAttempted': 25.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 8.5,
u'minsPlayed': 360,
u'name': u'Arsenal',
u'ranking': 2,
u'rating': 7.039793956043958,
u'apps': 4,
u'tackleWonTotal': 20.75,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Arsenal',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 13,
u'tournamentId': 2,
u'tackleTotalAttempted': 29.25
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 10.25,
u'minsPlayed': 360,
u'name': u'Swansea',
u'ranking': 3,
u'rating': 6.999136904761905,
u'apps': 4,
u'tackleWonTotal': 16.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Swansea',
u'seasonName': None,
u'regionCode': u'gb-wls',
u'teamId': 259,
u'tournamentId': 2,
u'tackleTotalAttempted': 26.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 7.75,
u'minsPlayed': 360,
u'name': u'Southampton',
u'ranking': 4,
u'rating': 6.945068681318677,
u'apps': 4,
u'tackleWonTotal': 16.75,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Southampton',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 18,
u'tournamentId': 2,
u'tackleTotalAttempted': 24.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 7.75,
u'minsPlayed': 360,
u'name': u'Leicester',
u'ranking': 5,
u'rating': 6.922499999999998,
u'apps': 4,
u'tackleWonTotal': 23.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Leicester',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 14,
u'tournamentId': 2,
u'tackleTotalAttempted': 31.0
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 4.75,
u'minsPlayed': 360,
u'name': u'AstonVilla',
u'ranking': 6,
u'rating': 6.909995421245423,
u'apps': 4,
u'tackleWonTotal': 19.75,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'AstonVilla',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 24,
u'tournamentId': 2,
u'tackleTotalAttempted': 24.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 8.0,
u'minsPlayed': 360,
u'name': u'CrystalPalace',
u'ranking': 7,
u'rating': 6.89214285714286,
u'apps': 4,
u'tackleWonTotal': 16.0,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'CrystalPalace',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 162,
u'tournamentId': 2,
u'tackleTotalAttempted': 24.0
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 7.75,
u'minsPlayed': 360,
u'name': u'Everton',
u'ranking': 8,
u'rating': 6.876071428571427,
u'apps': 4,
u'tackleWonTotal': 16.5,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Everton',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 31,
u'tournamentId': 2,
u'tackleTotalAttempted': 24.25
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 10.5,
u'minsPlayed': 360,
u'name': u'WestHam',
u'ranking': 9,
u'rating': 6.86017857142857,
u'apps': 4,
u'tackleWonTotal': 20.0,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'WestHam',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 29,
u'tournamentId': 2,
u'tackleTotalAttempted': 30.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 8.75,
u'minsPlayed': 360,
u'name': u'ManUtd',
u'ranking': 10,
u'rating': 6.855535714285715,
u'apps': 4,
u'tackleWonTotal': 19.5,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'ManchesterUnited',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 32,
u'tournamentId': 2,
u'tackleTotalAttempted': 28.25
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 7.0,
u'minsPlayed': 360,
u'name': u'Watford',
u'ranking': 11,
u'rating': 6.850975274725273,
u'apps': 4,
u'tackleWonTotal': 21.5,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Watford',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 27,
u'tournamentId': 2,
u'tackleTotalAttempted': 28.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 10.0,
u'minsPlayed': 360,
u'name': u'Liverpool',
u'ranking': 12,
u'rating': 6.85006868131868,
u'apps': 4,
u'tackleWonTotal': 20.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Liverpool',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 26,
u'tournamentId': 2,
u'tackleTotalAttempted': 30.25
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 7.5,
u'minsPlayed': 360,
u'name': u'Tottenham',
u'ranking': 13,
u'rating': 6.777403846153845,
u'apps': 4,
u'tackleWonTotal': 19.5,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Tottenham',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 30,
u'tournamentId': 2,
u'tackleTotalAttempted': 27.0
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 9.25,
u'minsPlayed': 360,
u'name': u'WestBrom',
u'ranking': 14,
u'rating': 6.69517857142857,
u'apps': 4,
u'tackleWonTotal': 18.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'WestBromwichAlbion',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 175,
u'tournamentId': 2,
u'tackleTotalAttempted': 27.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 9.25,
u'minsPlayed': 360,
u'name': u'Chelsea',
u'ranking': 15,
u'rating': 6.659107142857143,
u'apps': 4,
u'tackleWonTotal': 15.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Chelsea',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 15,
u'tournamentId': 2,
u'tackleTotalAttempted': 24.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 11.5,
u'minsPlayed': 360,
u'name': u'Sunderland',
u'ranking': 16,
u'rating': 6.656263736263735,
u'apps': 4,
u'tackleWonTotal': 26.75,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Sunderland',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 16,
u'tournamentId': 2,
u'tackleTotalAttempted': 38.25
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 10.25,
u'minsPlayed': 360,
u'name': u'Bournemouth',
u'ranking': 17,
u'rating': 6.64839285714286,
u'apps': 4,
u'tackleWonTotal': 17.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Bournemouth',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 183,
u'tournamentId': 2,
u'tackleTotalAttempted': 27.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 8.5,
u'minsPlayed': 360,
u'name': u'Stoke',
u'ranking': 18,
u'rating': 6.600714285714285,
u'apps': 4,
u'tackleWonTotal': 19.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Stoke',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 96,
u'tournamentId': 2,
u'tackleTotalAttempted': 27.75
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 7.75,
u'minsPlayed': 360,
u'name': u'Norwich',
u'ranking': 19,
u'rating': 6.59885989010989,
u'apps': 4,
u'tackleWonTotal': 17.75,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'Norwich',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 168,
u'tournamentId': 2,
u'tackleTotalAttempted': 25.5
},
{
u'seasonId': 0,
u'tournamentRegionId': 252,
u'challengeLost': 8.25,
u'minsPlayed': 360,
u'name': u'Newcastle',
u'ranking': 20,
u'rating': 6.596428571428575,
u'apps': 4,
u'tackleWonTotal': 18.25,
u'tournamentName': u'PremierLeague',
u'tournamentRegionCode': None,
u'teamName': u'NewcastleUnited',
u'seasonName': None,
u'regionCode': u'gb-eng',
u'teamId': 23,
u'tournamentId': 2,
u'tackleTotalAttempted': 26.5
} ] }"
这是我的完整代码:
import json
import requests
url = "http://www.whoscored.com/StatisticsFeed/1/GetTeamStatistics"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'www.whoscored.com',
'Referer': 'http://www.whoscored.com/'}
def GetResponse(category, subcategory):
params = {
'category': category,
'subcategory': subcategory,
'statsAccumulationType': '0',
'field': 'Overall',
'tournamentOptions': '',
'timeOfTheGameStart': '0',
'timeOfTheGameEnd': '5',
'stageId': '12496',
'sortBy': 'Rating',
'sortAscending': 'false',
'page': '1',
'includeZeroValues': '',
'numberOfTeamsToPick': '',
'isCurrent': 'true',
'formation': ''
}
return requests.get(url, params=params, headers=headers).json()
tackles = GetResponse('tackles','success');
parsedtackles = json.loads(str(tackles))
print tackles
现在我能解决这个问题了。感谢所有回复的人,你的回复对我帮助很大。你知道吗
事实上,这不是一个正确的JSON字符串。u'是Python unicode字符串对象,可能在那里您的原始JSON被转换成Python对象,然后被字符串化。你知道吗
检查更改的参与位置,然后消除它以提供正确的JSON to load()方法
正如其他人提到的,这不是JSON;除此之外,JSON需要它的键被双引号(而不是单引号)并禁止Python
u
前缀。你知道吗如果这是您实际获得的数据,那么最好使用
ast.literal_eval
。(Docs here.)也就是说,当你说你想跑
json.loads(str(tackles))
的时候,我会看得更远一点。你知道吗str(tackles)
不生成JSON;json.dumps(tackles)
生成。如果您只想测试JSON生成/解析,请尝试json.loads(json.dumps(tackles))
。你知道吗或者只是
相关问题 更多 >
编程相关推荐