有什么方法可以使simplejson变得不那么严格? [英] Is there any way to make simplejson less strict?
问题描述
我有兴趣让simplejson.loads()
成功解析以下内容:
I'm interested in having simplejson.loads()
successfully parse the following:
{foo:3}
它抛出一个JSONDecodeError
,上面写着期望属性名称",但实际上是在说我需要在属性名称周围用双引号引起来".这对于我的用例来说很烦人,我宁愿选择一个不太严格的行为.我已经阅读了文档,但是除了制作自己的解码器类之外,我看不到任何明显的东西可以改变这种行为.
It throws a JSONDecodeError
saying "expecting property name" but in reality it's saying "I require double quotes around my property names". This is annoying for my use case, and I'd prefer a less strict behavior. I've read the docs, but beyond making my own decoder class, I don't see anything obvious that changes this behavior.
推荐答案
您可以使用YAML(> = 1.2),因为它是JSON的超集,您可以执行以下操作:
You can use YAML (>=1.2)as it is a superset of JSON, you can do:
>>> import yaml
>>> s = '{foo: 8}'
>>> yaml.load(s)
{'foo': 8}
这篇关于有什么方法可以使simplejson变得不那么严格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!