[英]Python - JSONDecodeError: Expecting property name enclosed in double quotes
I've checked out the existing answers for this question and I can't find what my specific issue is.我已经检查了这个问题的现有答案,但我找不到我的具体问题是什么。 I'm trying to parse what I think is a valid JSON object, but somehow it's rejecting it.
我试图解析我认为是有效的 JSON 对象,但不知何故它拒绝了它。
I used this code to dump the data to the file from an API response:我使用此代码将数据从 API 响应转储到文件:
def write_video_ids_to_file(video_ids, video_data_file):
# Create file if it doesn't exist
file_is_empty = is_file_empty(video_data_file)
if file_is_empty:
open(video_data_file, 'w').close()
with open(video_data_file, 'a') as file:
json.dump(video_ids, file, indent=4, sort_keys=True, default=str)
print('collecting video ids')
And this code below that's running to try to load that JSON back out of the file seems to be failing:下面的这段代码正在运行以尝试从文件中加载该 JSON 似乎失败了:
def construct_video_id_array(video_data_file):
with open(video_data_file, 'r') as file:
json_data = json.load(file) # <-- Failing here
video_ids_separate = []
for video in json_data:
video_ids_separate.append(video['video_id'])
return video_ids_separate
Here's a sample of my JSON data (there's like 8 MB of it, so I'm not going to post it all):这是我的 JSON 数据示例(大约有 8 MB,所以我不打算全部发布):
{[
{
"channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
"collection_date": "2020-08-18 20:55:18.854967",
"publish_date": 1597784402.0,
"video_id": "5tBnaxQKpHQ"
},
{
"channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
"collection_date": "2020-08-18 20:55:18.854967",
"publish_date": 1597698002.0,
"video_id": "7J3H8ckUaYU"
}
]}
I originally didn't have the { at the beginning and end, so I added those.我最初在开头和结尾没有{ ,所以我添加了这些。 I am not sure if that was the right move, but for what it's worth, it's not working with or without it, just throwing different errors.
我不确定这是否是正确的举动,但就其价值而言,无论有没有它都不起作用,只是抛出不同的错误。
Your JSON content should look like:您的 JSON 内容应如下所示:
{ "your_data":[
{
"channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
"collection_date": "2020-08-18 20:55:18.854967",
"publish_date": 1597784402.0,
"video_id": "5tBnaxQKpHQ"
},
{
"channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
"collection_date": "2020-08-18 20:55:18.854967",
"publish_date": 1597698002.0,
"video_id": "7J3H8ckUaYU"
}
]}
This is valid.这是有效的。 Check this website out: https://www.geeksforgeeks.org/json-load-in-python/ and also https://jsonlint.com/ .
查看此网站: https://www.geeksforgeeks.org/json-load-in-python/以及https://jsonlint.com/ 。
All JSON data will need a key, even a list.所有 JSON 数据都需要一个键,甚至是一个列表。
Your JSON object should also look like:您的 JSON 对象也应如下所示:
{ "your_data":[
{
"channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
"collection_date": "2020-08-18 20:55:18.854967",
"publish_date": 1597784402.0,
"video_id": "5tBnaxQKpHQ"
},
{
"channel_id": "UC9CuvdOVfMPvKCiwdGKL3cQ",
"collection_date": "2020-08-18 20:55:18.854967",
"publish_date": 1597698002.0,
"video_id": "7J3H8ckUaYU"
}
]}
So file json.dump will dump the proper JSON content in a file.所以文件json.dump将在文件中转储正确的 JSON 内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.