为 JSONDecodeError 编写循环/异常

Posted

技术标签:

【中文标题】为 JSONDecodeError 编写循环/异常【英文标题】:Writing a loop/exception for JSONDecodeError 【发布时间】:2020-02-18 13:34:19 【问题描述】:

我正在尝试构建一个在 Tkinter GUI 中打印 JSON 结果的程序,但我遇到了以下问题: 如果发布的图片足够清晰可以得到结果怎么办?

我知道我可以使用 if、else 循环或异常,但我无法获得我想要的 GUI 的结果,它会告诉用户如果发生错误就会出现错误。

我在终端中遇到的错误是:

File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

到目前为止,我的代码是:

if: json.decoder.JSONDecodeError:
    oops = tk.Tk()
    window_width = oops.winfo_reqwidth()
    window_height = oops.winfo_reqheight()
    # Gets both half the screen width/height and window width/height
    position_right = int(oops.winfo_screenwidth()/2 - window_width/2)
    position_down = int(oops.winfo_screenheight()/2 - window_height/2)
    # Positions the window in the center of the page.
    oops.geometry("++".format(position_right, position_down))
    main = tk.Label(oops, text='Oops, your Photo could not be analyzed!', font='Verdana 70 bold').pack()
    oops.after(5000, oops.destroy)

else:
    write = file.write(data)
    pretty_json = json.loads(data)
    print(pretty_json)

我试过了,除了这样的例外,也没有得到我想要的结果:

try:
    write = file.write(data)
    pretty_json = json.loads(data)
    print(pretty_json)
except json.JSONDecodeError:
    oops = tk.Tk()
    window_width = oops.winfo_reqwidth()
    window_height = oops.winfo_reqheight()
    # Gets both half the screen width/height and window width/height
    position_right = int(oops.winfo_screenwidth()/2 - window_width/2)
    position_down = int(oops.winfo_screenheight()/2 - window_height/2)
    # Positions the window in the center of the page.
    oops.geometry("++".format(position_right, position_down))
    main = tk.Label(oops, text='Oops, your Photo could not be nalyzed!', font='Verdana 70 bold').pack()

任何帮助将不胜感激,谢谢!

【问题讨论】:

【参考方案1】:

这段代码解决了我的问题:我需要稍后在我的脚本中添加一个异常,以及在打印结果时。

另外,我愚蠢地忘记添加mainloop

try:
    global name
    name = date_time + "_result.json"
    file = open(name, 'wb')
    global data
    data = response.content
    write = file.write(data)
    pretty_json = json.loads(data)
    print(pretty_json)
except JSONDecodeError:
    oops = tk.Tk()
    window_width = oops.winfo_reqwidth()
    window_height = oops.winfo_reqheight()
    # Gets both half the screen width/height and window width/height
    position_right = int(window_width/2)
    position_down = int(window_height/2)
    # Positions the window in the center of the page.
    oops.geometry("++".format(position_right, position_down))
    main = tk.Label(oops, text='Oops!', font='Verdana 80 bold').pack()
    detail = tk.Label(oops, text='Your Photo could not be analyzed', font='Verdana 60').pack()
    oops.after(5000, oops.destroy)
    oops.mainloop()

【讨论】:

以上是关于为 JSONDecodeError 编写循环/异常的主要内容,如果未能解决你的问题,请参考以下文章

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

PythonAnywhere 上的 JSONDecodeError

Python google-trans-new translate 引发错误:JSONDecodeError:额外数据:

pythonjson.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)!!!

json.decoder.JSONDecodeError:期望值:尝试写入json文件时,第1行第1列(字符0)

如何为循环编写异常?