如何在python中解码无效的json字符串 [英] How to decode an invalid json string in python

查看:109
本文介绍了如何在python中解码无效的json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以解码类似JSON的字符串.

I wonder if there is a way to decode a JSON-like string.

我得到了字符串:

'{ hotel: { id: "123", name: "hotel_name"} }'

这不是有效的JSON字符串,因此我无法使用python API直接对其进行解码. Python将仅接受字符串化的JSON字符串,例如:

It's not a valid JSON string, so I can't decode it directly with the python API. Python will only accept a stringified JSON string like:

 '{ "hotel": { "id": "123", "name": "hotel_name"} }'

其中的属性被引用为字符串.

where properties are quoted to be a string.

推荐答案

使用 demjson 模块,它可以在非严格模式下进行解码.

Use demjson module, which has ability to decode in non-strict mode.

In [1]: import demjson
In [2]: demjson.decode('{ hotel: { id: "123", name: "hotel_name"} }')
Out[2]: {u'hotel': {u'id': u'123', u'name': u'hotel_name'}}

这篇关于如何在python中解码无效的json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆