相关疑难解决方法(0)

Python 无法解析带有额外尾随逗号的 JSON

这段代码:

import json
s = '{ "key1": "value1", "key2": "value2", }'
json.loads(s)
Run Code Online (Sandbox Code Playgroud)

在 Python 2 中产生此错误:

ValueError:期望属性名称:第 1 行第 16 列(字符 15)

Python 3 中的类似结果:

json.decoder.JSONDecodeError:期望用双引号括起来的属性名称:第 1 行第 16 列(字符 15)

如果我删除尾随逗号(在 之后"value2"),则不会出现错误。但是我的代码会处理许多不同的 JSON,所以我无法手动完成。是否可以将解析器设置为忽略这样的最后一个逗号?

python parsing json runtime-error

6
推荐指数
2
解决办法
3457
查看次数

Lenient JSON Parser for Python

Is there a "lenient" JSON Parser for Python?

I keep getting (handwritten) JSON files such as this:

/* This JSON file is created by someone who does not know JSON
   And not competent enough to search about "JSON Validators" */

{

  /* Hey look!
     A honkin' block comment here!
     Yeehaw */

  "key1": "value1",  // Hey look there's a standard-breaking comment here!
  "key3": .65,       // I'm too lazy to type "0"
  "key4": -.75,      // That "other" .Net program works anyways...
  "key5": …
Run Code Online (Sandbox Code Playgroud)

python json

6
推荐指数
1
解决办法
587
查看次数

标签 统计

json ×2

python ×2

parsing ×1

runtime-error ×1