JSON 来自 (RIOT) API 格式不正确
JSON from (RIOT) API Formatted Incorrectly
我正在将 JSON 数据从 API 和 运行 导入到 Python,出现以下解码错误:
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
查看在线示例立即清楚我的 JSON 数据有 '
而其他人有 "
.
理想情况下,我想知道为什么以这种方式下载它。这很可能是我的错误,而不是他们的错误。
我认为更正 JSON 格式应该很容易,但我在这里也失败了。请参阅下面的代码,了解我如何获取 JSON 数据以及我修复它的尝试。
#----------------------------------------
#---Read this only if you want to download
#---the data yourself.
#----------------------------------------
#Built from 'Towards Data Science' guide
#https://towardsdatascience.com/how-to-use-riot-api-with-python-b93be82dbbd6
#Must first have installed riotwatcher
#Info in my example is made up, I can't supply a real API code or
#I get in trouble. Sorry about this. You could obtain one from their website
#but this would be a lot of faff for what is probably a simple Whosebug
#question
#If you were to get/have a key you could use the following information:
#<EUW> for region
#<Agurin> for name
#----------------------------------------
#---Code
#----------------------------------------
#--->Set Variables
#Get installed riotwatcher module for
#Python
import riotwatcher
#Import riotwatcher tools.
from riotwatcher import LolWatcher, ApiError
#Import JSON (to read the JSON API file)
import json
# Global variables
# Get new API from
# https://developer.riotgames.com/
api_key = 'RGAPI-XXXXXXX-XXX-XXXX-XXXX-XXXX'
watcher = LolWatcher(api_key)
my_region = 'MiddleEarth'
#need to give path to where records
#are to be stored
records_dir = "/home/solebaysharp/Projects/Riot API/Records"
#--->Obtain initial data, setup new varaibles
#Use 'watcher' to get basic stats and setup my account as a variable (me)
me = watcher.summoner.by_name(my_region, "SolebaySharp")
# Setup retrieval of recent match info
my_matches = watcher.match.matchlist_by_account(my_region, me["accountId"])
print(my_matches)
#--->Download the recent match data
#Define where the JSON data is going to go
recent_matches_index_json = (records_dir + "/recent_matches_index.json")
#get that JSON data
print ("Downloading recent match history data")
file_handle = open(recent_matches_index_json,"w+")
file_handle.write(str(my_matches))
file_handle.close()
#convert it to python
file_handle = open(recent_matches_index_json,)
recent_matches_index = json.load(file_handle)
除此之外,出现以下错误...
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
所以为了更正这个问题,我尝试了:
file_handle = open(recent_matches_index_json)
json_sanitised = json.loads(file_handle.replace("'", '"'))
这个returns...
AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'
为了完整起见,下面是 JSON 的样例。我添加了段落以增强可读性。它不是这样的。
{'matches': [
{'platformId': 'NA1',
'gameId': 5687555181,
'champion': 235,
'queue': 400,
'season': 13,
'timestamp': 1598243995076,
'role': 'DUO_SUPPORT',
'lane': 'BOTTOM'
},
{'platformId': 'NA1',
'gameId': 4965733458,
'champion': 235,
'queue': 400,
'season': 13,
'timestamp': 1598240780841,
'role': 'DUO_SUPPORT',
'lane': 'BOTTOM'
},
{'platformId': 'NA1',
'gameId': 4583215645,
'champion': 111,
'queue': 400,
'season': 13,
'timestamp': 1598236666162,
'role': 'DUO_SUPPORT',
'lane': 'BOTTOM'
}],
'startIndex': 0,
'endIndex': 100,
'totalGames': 186}
这是因为 Python 正在将 JSON 转换为字符串 (str
)。
file_handle.write(str(my_matches))
因为它看不出 '
和 "
之间的区别,所以它只使用默认值 '
。
我们可以使用 JSON.dumps
来阻止这种情况的发生。这部分(当然不是完全)也回答了问题的第二部分,因为我们使用的是格式正确的 JSON 命令。
above-mentioned 行只需替换为:
file_handle.write(json.dumps(my_matches))
这将保留 JSON 格式。
我正在将 JSON 数据从 API 和 运行 导入到 Python,出现以下解码错误:
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
查看在线示例立即清楚我的 JSON 数据有 '
而其他人有 "
.
理想情况下,我想知道为什么以这种方式下载它。这很可能是我的错误,而不是他们的错误。
我认为更正 JSON 格式应该很容易,但我在这里也失败了。请参阅下面的代码,了解我如何获取 JSON 数据以及我修复它的尝试。
#----------------------------------------
#---Read this only if you want to download
#---the data yourself.
#----------------------------------------
#Built from 'Towards Data Science' guide
#https://towardsdatascience.com/how-to-use-riot-api-with-python-b93be82dbbd6
#Must first have installed riotwatcher
#Info in my example is made up, I can't supply a real API code or
#I get in trouble. Sorry about this. You could obtain one from their website
#but this would be a lot of faff for what is probably a simple Whosebug
#question
#If you were to get/have a key you could use the following information:
#<EUW> for region
#<Agurin> for name
#----------------------------------------
#---Code
#----------------------------------------
#--->Set Variables
#Get installed riotwatcher module for
#Python
import riotwatcher
#Import riotwatcher tools.
from riotwatcher import LolWatcher, ApiError
#Import JSON (to read the JSON API file)
import json
# Global variables
# Get new API from
# https://developer.riotgames.com/
api_key = 'RGAPI-XXXXXXX-XXX-XXXX-XXXX-XXXX'
watcher = LolWatcher(api_key)
my_region = 'MiddleEarth'
#need to give path to where records
#are to be stored
records_dir = "/home/solebaysharp/Projects/Riot API/Records"
#--->Obtain initial data, setup new varaibles
#Use 'watcher' to get basic stats and setup my account as a variable (me)
me = watcher.summoner.by_name(my_region, "SolebaySharp")
# Setup retrieval of recent match info
my_matches = watcher.match.matchlist_by_account(my_region, me["accountId"])
print(my_matches)
#--->Download the recent match data
#Define where the JSON data is going to go
recent_matches_index_json = (records_dir + "/recent_matches_index.json")
#get that JSON data
print ("Downloading recent match history data")
file_handle = open(recent_matches_index_json,"w+")
file_handle.write(str(my_matches))
file_handle.close()
#convert it to python
file_handle = open(recent_matches_index_json,)
recent_matches_index = json.load(file_handle)
除此之外,出现以下错误...
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
所以为了更正这个问题,我尝试了:
file_handle = open(recent_matches_index_json)
json_sanitised = json.loads(file_handle.replace("'", '"'))
这个returns...
AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'
为了完整起见,下面是 JSON 的样例。我添加了段落以增强可读性。它不是这样的。
{'matches': [
{'platformId': 'NA1',
'gameId': 5687555181,
'champion': 235,
'queue': 400,
'season': 13,
'timestamp': 1598243995076,
'role': 'DUO_SUPPORT',
'lane': 'BOTTOM'
},
{'platformId': 'NA1',
'gameId': 4965733458,
'champion': 235,
'queue': 400,
'season': 13,
'timestamp': 1598240780841,
'role': 'DUO_SUPPORT',
'lane': 'BOTTOM'
},
{'platformId': 'NA1',
'gameId': 4583215645,
'champion': 111,
'queue': 400,
'season': 13,
'timestamp': 1598236666162,
'role': 'DUO_SUPPORT',
'lane': 'BOTTOM'
}],
'startIndex': 0,
'endIndex': 100,
'totalGames': 186}
这是因为 Python 正在将 JSON 转换为字符串 (str
)。
file_handle.write(str(my_matches))
因为它看不出 '
和 "
之间的区别,所以它只使用默认值 '
。
我们可以使用 JSON.dumps
来阻止这种情况的发生。这部分(当然不是完全)也回答了问题的第二部分,因为我们使用的是格式正确的 JSON 命令。
above-mentioned 行只需替换为:
file_handle.write(json.dumps(my_matches))
这将保留 JSON 格式。