如何在Python中从json.loads获取错误位置

2024-05-19 07:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在Python 3中使用json.loads并捕获任何结果错误时,例如:

try:
  data = json.loads(string)
except ValueError as err:
  print(err)

我得到一个有用的信息,比如:

Expecting ',' delimiter: line 12 column 12 (char 271)

我希望能够向用户显示这一点,以及导致问题的确切位置(我正在阅读用户编写的JSON)。我怎样才能脱离这个行列?

我可以在err上使用regex,但这感觉不是个好主意,因为我不知道这个消息是否是国际化的,并且可能在不同版本的python中发生变化。有更好的办法吗?


Tags: 用户信息jsondatastringas错误err
3条回答

如果json对象很小,请在此处越过json对象 http://jsonlint.com/ 它给出了json中断的位置。

扫描json/decoder.py source code,我们可以看到解码器的错误消息是使用errmsg函数构造的:

def errmsg(msg, doc, pos, end=None):
    # Note that this function is called from _json
    lineno, colno = linecol(doc, pos)
    if end is None:
        fmt = '{0}: line {1} column {2} (char {3})'
        return fmt.format(msg, lineno, colno, pos)
        #fmt = '%s: line %d column %d (char %d)'
        #return fmt % (msg, lineno, colno, pos)
    endlineno, endcolno = linecol(doc, end)
    fmt = '{0}: line {1} column {2} - line {3} column {4} (char {5} - {6})'
    return fmt.format(msg, lineno, colno, endlineno, endcolno, pos, end)
    #fmt = '%s: line %d column %d - line %d column %d (char %d - %d)'
    #return fmt % (msg, lineno, colno, endlineno, endcolno, pos, end)

因为这是一个纯python模块,所以很容易用自定义的函数包装这个函数。这个过程称为monkey patching

import json

original_errmsg= json.decoder.errmsg

def our_errmsg(msg, doc, pos, end=None):
    json.last_error_position= json.decoder.linecol(doc, pos)
    return original_errmsg(msg, doc, pos, end)

json.decoder.errmsg= our_errmsg

try:
    data = json.loads('{1:}')
except ValueError as e:
    print("error at", json.last_error_position)

显然,这个解决方案并不理想,因为实现可能随时更改,尽管它仍然比依赖消息要好。在修补之前,您应该检查errmsg是否存在(并且可能没有其他参数,或者使用varargs)。

如果您使用simplejson库,您将获得一个合格的JSONDecodeError

class JSONDecodeError(ValueError):
   """Subclass of ValueError with the following additional properties:

   msg: The unformatted error message
   doc: The JSON document being parsed
   pos: The start index of doc where parsing failed
   end: The end index of doc where parsing failed (may be None)
   lineno: The line corresponding to pos
   colno: The column corresponding to pos
   endlineno: The line corresponding to end (may be None)
   endcolno: The column corresponding to end (may be None)

   """

希望,这将是merged into stdlib很快。

相关问题 更多 >

    热门问题