【问题标题】:What is wrong with this JSON string?这个 JSON 字符串有什么问题?
【发布时间】:2015-12-04 17:54:12
【问题描述】:

这是我第一次使用 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

现在我能够解决问题。感谢所有回复的人,你们的回复对我帮助很大。

【问题讨论】:

  • 键上的 u 前缀不是有效的 json。这看起来像是有人漂亮地打印了一本 python 字典。
  • 正如其他人指出的那样,它不是有效的 json。这些值需要用双引号括起来。您可以使用它来验证您的 json jsonlint.com

标签: python json python-2.7 parsing web-scraping


【解决方案1】:

事实是这不是一个正确的 JSON 字符串。 u' 用于 Python unicode 字符串对象,可能在某处将您的原始 JSON 转换为 Python 对象,然后进行字符串化。

检查更改发生的位置,然后消除它以提供适当的 JSON 到 load() 方法

【讨论】:

  • 感谢您的回复。我将完整代码添加到问题详细信息中。你知道这发生在哪里吗?
  • hm.. 由于 [请求文档[(python-requests.org/en/latest/user/quickstart/…) 这是从 requests.get(...).json() 返回的 JSON 格式。而不是这个,可以试试import json import requests ... response = requests.get(...) json_data = json.loads(response.text) 抱歉没有换行符
【解决方案2】:

正如其他人所提到的,这不是 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))

【讨论】:

  • 谢谢,现在有了 json.loads(json.dumps(tackles)) 这不会再导致错误了。现在请我如何在字典中查找说第 3 队的 teamName?
  • parsedtackles["teamTableStats"][2]["teamName"]
【解决方案3】:

或者干脆

parsedtackles = str (json.dumps(tackles))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-31
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多