如何在Python中忽略无效的JSON并用None替换无效对象?

时间:2017-09-17 18:52:33

标签: python json

我想将JSON树转换为Python字典。以下是我可能收到的JSON树的示例。

{"tree":{"arbitrary_text": "foo", "arbitrary_function": function helloworld() {console.log("hello world!")}}

正如预期的那样,尝试使用

进行解码时
json_obj = json.loads(raw_json)

a

json.decoder.JSONDecodeError

被抛出。

JSON是任意的,因此解析我想要的信息的唯一可能方法是用某些东西替换无效的JSON,或者只是不在Python树中包含无效的对象。是否可以使用内置的JSON模块执行此操作(无需更改自己的代码)。如果没有,那么我可以使用一个模块吗?

我不熟悉黑客内置模块,所以我正在寻找一个直接的解决方案。]

这就是我想要实现的目标(Python Dictionary):

{'tree': {'arbitrary_text': 'foo', 'arbitrary_function': None}

OR

{'tree': {'arbitrary_text': 'foo', 'arbitrary_function': 'function helloworld() {console.log("hello world!")}'}

0 个答案:

没有答案