首页 > 解决方案 > 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}

标签: pythonjson

解决方案


推荐阅读