Displaying better error message than “No JSON object could be decoded”

后端 未结 11 1062
既然无缘
既然无缘 2020-11-30 23:05

Python code to load data from some long complicated JSON file:

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

(note: the be

11条回答
  •  失恋的感觉
    2020-12-01 00:03

    You could try the rson library found here: http://code.google.com/p/rson/ . I it also up on PYPI: https://pypi.python.org/pypi/rson/0.9 so you can use easy_install or pip to get it.

    for the example given by tom:

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

    RSON is a designed to be a superset of JSON, so it can parse JSON files. It also has an alternate syntax which is much nicer for humans to look at and edit. I use it quite a bit for input files.

    As for the capitalizing of boolean values: it appears that rson reads incorrectly capitalized booleans as strings.

    >>> rson.loads('[true,False]')
    [True, u'False']
    

提交回复
热议问题