Python can't parse JSON with extra trailing comma

前端 未结 6 894
梦如初夏
梦如初夏 2020-12-21 03:59

This code:

import json
s = \'{ \"key1\": \"value1\", \"key2\": \"value2\", }\'
json.loads(s)

produces this error in Python 2:

6条回答
  •  天涯浪人
    2020-12-21 04:13

    That's because an extra , is invalid according to JSON standard.

    An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

    If you really need this, you could wrap python's json parser with jsoncomment. But I would try to fix JSON in the origin.

提交回复
热议问题