You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python程序JSON解析失败,JSONDecodeError问题排查求助

问题:Python程序触发JSONDecodeError,非.json后缀URL无法解析

数月前编写的Python程序此前运行正常,如今执行时触发JSONDecodeError。尝试多个非.json后缀的URL均无法正常解析,但带有.json扩展名的URL可正常工作。

原代码

import sys
import random
import pyfiglet
import requests
import json
list=[]
if len(sys.argv)<=3:
    responce=requests.get('https://github.com/pwaller/pyfiglet/tree/main/pyfiglet/fonts-standard')
    o=responce.json()
    x=o["payload"]["tree"]["items"]
    for i in x:
       list.append(i["name"])
    responce1=requests.get('https://github.com/pwaller/pyfiglet/tree/main/pyfiglet/fonts-contrib')
    o1=responce1.json()
    y=o1["payload"]["tree"]["items"]
    for i1 in y:
       list.append(i1["name"])
    if len(sys.argv)==1:
        z=random.choice(list)
        z=z[0:-4]
        v=input('Input: ')
        print('Output:\n'+pyfiglet.figlet_format(v, font=z))
        sys.exit()
    if len(sys.argv)==3:
        if (sys.argv[1]=='-f' or sys.argv[1]=='--font'):
            for font in list:
                if font[0:-4]==sys.argv[2]:
                    n=input('Input: ')
                    print('Output:\n'+pyfiglet.figlet_format(n, font=sys.argv[2]))
                    sys.exit()
                else:
                    continue
            sys.exit('Invalid usage')
        else:
            sys.exit('Invalid usage')
    else:
        sys.exit('Invalid usage')
else:
    sys.exit('Invalid usage')    

报错信息

Traceback (most recent call last):
  File "C:\Users\tonib\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tonib\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tonib\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tonib\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 7 column 1 (char 6)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\pythonproj\indoor.py", line 9, in <module>
    o=responce.json()
      ^^^^^^^^^^^^^^^
  File "C:\Users\tonib\AppData\Local\Programs\Python\Python312\Lib\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 7 column 1 (char 6)

解决方案

问题原因

你请求的GitHub页面URL返回的是HTML网页,不是JSON数据。之前GitHub可能在页面中嵌入了可解析的JSON payload,但现在接口逻辑变更,直接请求网页无法再获取有效JSON。必须使用官方提供的GitHub API接口来获取仓库内容的JSON数据。

修正步骤

  1. 替换请求URL:使用GitHub API的仓库内容端点,替换原有的网页URL:
    • 原https://github.com/pwaller/pyfiglet/tree/main/pyfiglet/fonts-standard → 新https://api.github.com/repos/pwaller/pyfiglet/contents/pyfiglet/fonts-standard
    • 原https://github.com/pwaller/pyfiglet/tree/main/pyfiglet/fonts-contrib → 新https://api.github.com/repos/pwaller/pyfiglet/contents/pyfiglet/fonts-contrib
  2. 调整JSON解析逻辑:GitHub API返回的是直接包含文件信息的数组,无需再读取payload层级。
  3. 避免使用内置关键字:把变量名list改为font_list,避免覆盖Python内置的list类型。

修正后的代码

import sys
import random
import pyfiglet
import requests

font_list = []
if len(sys.argv) <= 3:
    # 请求GitHub API获取标准字体列表
    response = requests.get('https://api.github.com/repos/pwaller/pyfiglet/contents/pyfiglet/fonts-standard')
    items = response.json()
    for item in items:
        font_list.append(item["name"])
    
    # 请求GitHub API获取贡献字体列表
    response1 = requests.get('https://api.github.com/repos/pwaller/pyfiglet/contents/pyfiglet/fonts-contrib')
    items1 = response1.json()
    for item1 in items1:
        font_list.append(item1["name"])
    
    if len(sys.argv) == 1:
        # 随机选择字体
        selected_font = random.choice(font_list)[:-4]  # 去掉后缀
        user_input = input('Input: ')
        print('Output:\n' + pyfiglet.figlet_format(user_input, font=selected_font))
        sys.exit()
    
    if len(sys.argv) == 3:
        if sys.argv[1] in ('-f', '--font'):
            target_font = sys.argv[2]
            # 检查字体是否存在
            for font in font_list:
                if font[:-4] == target_font:
                    user_input = input('Input: ')
                    print('Output:\n' + pyfiglet.figlet_format(user_input, font=target_font))
                    sys.exit()
            sys.exit('Invalid usage: Font not found')
        else:
            sys.exit('Invalid usage: Unknown option')
    else:
        sys.exit('Invalid usage: Wrong number of arguments')
else:
    sys.exit('Invalid usage: Too many arguments')

内容的提问来源于stack exchange,提问作者user25991606

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 17:02:34