python - Json load 给了我一个 JSONDecodeError: Expecting property name 仅在终端中用双引号括起来
问题描述
我正在尝试通过终端参数传递 json:
import json
import sys
import requests
import platform
from winreg import *
from pathlib import Path
import datetime
def show_gifs(giffavs, folder):
value = json.loads(giffavs)["_state"]["favorites"]
print(f'found {len(value)} faved gifs')
for count, el in enumerate(value):
print(f'➡{count}: {el["url"]}')
if el["url"].split('.')[-1] != 'gif':
el["url"] += '.gif'
download_gif(el["url"], folder)
...
if __name__ == '__main__':
#set time value and get JSON from args if exist
now = datetime.datetime.now()
data = str(sys.argv[1]) #here I get my JSON data I used to just make it a str value but now I want to take it from gicen parameters
#look for right folder and create it
if len(sys.argv) < 3:
if len(sys.argv) == 3:
folder = sys.argv[2]
if platform.system() == 'Windows':
with OpenKey(HKEY_CURRENT_USER, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders') as key:
folder = QueryValueEx(key, '{374DE290-123F-4565-9164-39C4925E467B}')[0]
else:
folder = str(Path.home()) + '/Downloads'
folder += '/DiscordFavoriteGif(' + now.strftime("%d") + '.' + now.strftime("%b") + '.' + now.strftime(
"%Y") + '_' + now.strftime("%H") + now.strftime("%M") + now.strftime("%S") + ')'
Path(folder).mkdir(parents=True, exist_ok=True)
show_gifs(data, folder)
else:
print('too many arguments')
当我在 data 变量中手动编写我的 json 时,
data = 'MYJSON'
它可以工作,但是当我尝试在终端中使用相同的数据时,我
python DownloadFavoriteGIF.py MYJSON
得到一个json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
这很奇怪,因为它是完全相同的数据,你知道它可能来自哪里吗?
编辑
我的 json 看起来像这样我刚刚删除了隐私问题的 url:
{"_state":{"favorites":[{"url":"url","src":" url","width":1032,"height":1000,"format":"IMAGE"},{"url":" url","src":" url ","width":247,"height":387,"format":"IMAGE"},{"url":" url ","src":" url ","width":640,"height":358,"format":"VIDEO"},{"url":" url ","src":" url ","width":640,"height":516,"format":"VIDEO"},{"width":632,"height":640,"src":" url ","format":"VIDEO"}],"timesFavorited":6},"_version":2}
解决方案
推荐阅读
- angular - Angular 和 OpenLayers,点击时产生特写
- discord.js - Discord.js (v13) - 使用下拉菜单编辑消息
- visual-c++ - 通过虚表获取虚函数指针
- python-3.x - docplex.cp.model 比穷举搜索慢
- codeigniter - 在 Codeigniter 4 上处理 LDAP 无效凭据
- javascript - Gremlin查询以获取javascript中所有互连的顶点值
- javascript - React - 渲染组件数组中的问题
- java - 如何通过名称获取 vars 的值
- rust - 为什么编译器错误会抱怨多个可变引用而不是悬空引用?
- c++ - 使用 CGAL 的基本功能,例如 `square`