132

从一些长而复杂的 JSON 文件中加载数据的 Python 代码:

with open(filename, "r") as f:
  data = json.loads(f.read())

(注意:最好的代码版本应该是:

with open(filename, "r") as f:
  data = json.load(f)

但两者都表现出相似的行为)

对于许多类型的 JSON 错误(缺少分隔符、字符串中不正确的反斜杠等),这会打印一条非常有用的消息,其中包含发现 JSON 错误的行号和列号。

但是,对于其他类型的 JSON 错误(包括经典的“在列表中的最后一项上使用逗号”,以及其他诸如大写 true/false 之类的事情),Python 的输出只是:

Traceback (most recent call last):
  File "myfile.py", line 8, in myfunction
    config = json.loads(f.read())
  File "c:\python27\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "c:\python27\lib\json\decoder.py", line 360, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\python27\lib\json\decoder.py", line 378, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

对于这种类型的 ValueError,如何让 Python 告诉您 JSON 文件中的错误在哪里?

4

11 回答 11

180

我发现simplejson在许多内置json模块含糊不清的情况下,该模块会给出更多描述性错误。例如,对于列表中最后一项后有逗号的情况:

json.loads('[1,2,]')
....
ValueError: No JSON object could be decoded

这不是很具有描述性。与 相同的操作simplejson

simplejson.loads('[1,2,]')
...
simplejson.decoder.JSONDecodeError: Expecting object: line 1 column 5 (char 5)

好多了!同样对于其他常见错误,如大写True.

于 2013-02-19T11:33:45.710 回答
15

您将无法让 python 告诉您 JSON 不正确的位置。您将需要在这样的地方在线使用 linter

这将在您尝试解码的 JSON 中显示错误。

于 2013-02-15T16:58:05.373 回答
7

你可以试试这里的 rson 库:http ://code.google.com/p/rson/ 。我也在 PYPI 上:https ://pypi.python.org/pypi/rson/0.9所以你可以使用 easy_install 或 pip 来获取它。

对于汤姆给出的例子:

>>> rson.loads('[1,2,]')
...
rson.base.tokenizer.RSONDecodeError: Unexpected trailing comma: line 1, column 6, text ']'

RSON 被设计为 JSON 的超集,因此它可以解析 JSON 文件。它还有一种替代语法,便于人们查看和编辑。我将它用于输入文件。

至于布尔值的大写:似乎 rson 将错误地大写的布尔值读取为字符串。

>>> rson.loads('[true,False]')
[True, u'False']
于 2013-02-22T21:07:44.410 回答
4

我有一个类似的问题,这是由于单引号。JSON 标准 ( http://json.org ) 只讨论使用双引号,因此 pythonjson库必须只支持双引号。

于 2013-08-24T06:20:33.327 回答
3
于 2014-12-22T01:29:51.790 回答
3

As to me, my json file is very large, when use common json in python it gets the above error.

After install simplejson by sudo pip install simplejson.

And then I solved it.

import json
import simplejson


def test_parse_json():
    f_path = '/home/hello/_data.json'
    with open(f_path) as f:
        # j_data = json.load(f)      # ValueError: No JSON object could be decoded
        j_data = simplejson.load(f)  # right
    lst_img = j_data['images']['image']
    print lst_img[0]


if __name__ == '__main__':
    test_parse_json()
于 2018-08-02T08:28:48.883 回答
1

I had a similar problem this was my code:

    json_file=json.dumps(pyJson)
    file = open("list.json",'w')
    file.write(json_file)  

    json_file = open("list.json","r")
    json_decoded = json.load(json_file)
    print json_decoded

the problem was i had forgotten to file.close() I did it and fixed the problem.

于 2015-10-09T12:25:31.950 回答
0

The accepted answer is the easiest one to fix the problem. But in case you are not allowed to install the simplejson due to your company policy, I propose below solution to fix the particular issue of "using comma on the last item in a list":

  1. Create a child class "JSONLintCheck" to inherite from class "JSONDecoder" and override the init method of the class "JSONDecoder" like below:

    def __init__(self, encoding=None, object_hook=None, parse_float=None,parse_int=None, parse_constant=None, strict=True,object_pairs_hook=None)        
            super(JSONLintCheck,self).__init__(encoding=None, object_hook=None,      parse_float=None,parse_int=None, parse_constant=None, strict=True,object_pairs_hook=None)
            self.scan_once = make_scanner(self)
    
  1. make_scanner is a new function that used to override the 'scan_once' method of the above class. And here is code for it:</li>
  1 #!/usr/bin/env python
  2 from json import JSONDecoder
  3 from json import decoder
  4 import re
  5
  6 NUMBER_RE = re.compile(
  7     r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
  8     (re.VERBOSE | re.MULTILINE | re.DOTALL))
  9
 10 def py_make_scanner(context):
 11     parse_object = context.parse_object
 12     parse_array = context.parse_array
 13     parse_string = context.parse_string
 14     match_number = NUMBER_RE.match
 15     encoding = context.encoding
 16     strict = context.strict
 17     parse_float = context.parse_float
 18     parse_int = context.parse_int
 19     parse_constant = context.parse_constant
 20     object_hook = context.object_hook
 21     object_pairs_hook = context.object_pairs_hook
 22
 23     def _scan_once(string, idx):
 24         try:
 25             nextchar = string[idx]
 26         except IndexError:
 27             raise ValueError(decoder.errmsg("Could not get the next character",string,idx))
 28             #raise StopIteration
 29
 30         if nextchar == '"':
 31             return parse_string(string, idx + 1, encoding, strict)
 32         elif nextchar == '{':
 33             return parse_object((string, idx + 1), encoding, strict,
 34                 _scan_once, object_hook, object_pairs_hook)
 35         elif nextchar == '[':
 36             return parse_array((string, idx + 1), _scan_once)
 37         elif nextchar == 'n' and string[idx:idx + 4] == 'null':
 38             return None, idx + 4
 39         elif nextchar == 't' and string[idx:idx + 4] == 'true':
 40             return True, idx + 4
 41         elif nextchar == 'f' and string[idx:idx + 5] == 'false':
 42             return False, idx + 5
 43
 44         m = match_number(string, idx)
 45         if m is not None:
 46             integer, frac, exp = m.groups()
 47             if frac or exp:
 48                 res = parse_float(integer + (frac or '') + (exp or ''))
 49             else:
 50                 res = parse_int(integer)
 51             return res, m.end()
 52         elif nextchar == 'N' and string[idx:idx + 3] == 'NaN':
 53             return parse_constant('NaN'), idx + 3
 54         elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity':
 55             return parse_constant('Infinity'), idx + 8
 56         elif nextchar == '-' and string[idx:idx + 9] == '-Infinity':
 57             return parse_constant('-Infinity'), idx + 9
 58         else:
 59             #raise StopIteration   # Here is where needs modification
 60             raise ValueError(decoder.errmsg("Expecting propert name enclosed in double quotes",string,idx))
 61     return _scan_once
 62
 63 make_scanner = py_make_scanner
  1. Better put the 'make_scanner' function together with the new child class into a same file.
于 2018-04-20T08:30:17.567 回答
0

Just hit the same issue and in my case the problem was related to BOM (byte order mark) at the beginning of the file.

json.tool would refuse to process even empty file (just curly braces) until i removed the UTF BOM mark.

What I have done is:

  • opened my json file with vim,
  • removed byte order mark (set nobomb)
  • save file

This resolved the problem with json.tool. Hope this helps!

于 2018-09-07T18:33:01.873 回答
-2

When your file is created. Instead of creating a file with content is empty. Replace with:

json.dump({}, file)
于 2019-02-21T03:06:27.007 回答
-3

你可以使用cjson,它声称比纯 python 实现快 250 倍,因为你有“一些很长的复杂 JSON 文件”并且你可能需要运行它几次(解码器失败并报告他们的第一个错误只遇到)。

于 2013-02-26T09:26:26.617 回答