Python can't parse JSON with extra trailing comma

前端 未结 6 879
梦如初夏
梦如初夏 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:12

    Another option is to parse it as YAML; YAML accepts valid JSON but also accepts all sorts of variations.

    import yaml
    s = '{ "key1": "value1", "key2": "value2", }'
    yaml.load(s)
    

提交回复
热议问题