Python中json.loads的时候出错->要注意要解码的Json字符的编码

Python中json.loads的时候出错->要注意要解码的Json字符的编码

记录一些关于Python中使用json.loads时候的注意事项。

在贴出注意事项之前,先贴上,python文档中,关于json.loads的说明:

json. loads ( s[,  encoding[,  cls[,  object_hook[,  parse_float[, parse_int[,  parse_constant[,  object_pairs_hook[,  **kw]]]]]]]] )

Deserialize s (a str or unicode instance containing a JSON document) to a Python object.

If s is a str instance and is encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified.Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to unicode first.

The other arguments have the same meaning as in load().

 

1.如果传入的字符串的编码不是UTF-8的话,需要用encoding指定字符编码

对于:

dataDict = json.loads(dataJsonStr);

其中dataJsonStr是json字符串,如果其编码本身是非UTF-8的话,比如是GB2312的,那么上述代码,就会导致出错。改为对应的:

dataDict = json.loads(dataJsonStr, encoding="GB2312");

就可以了。

此处,即对应着上面函数解释中的:

If s is a str instance and is encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified

 

2.如果要解析的字符串,本身的编码类型,不是基于ASCII的,那么,调用json.loads之前,需要先将对应字符串,转换为Unicode类型的

还是以上述的:

dataDict = json.loads(dataJsonStr, encoding="GB2312");

为例,即使你此处的字符串dataJsonStr,已经通过encoding指定了合适的编码,但是由于其中,包含了其他的编码的字符,比如我本身dataJsonStr是GB2312的字符,但是其中又包含了的一些日文字符,此时,json.loads还是会出错,因为此处的dataJsonStr不是以ASCII为基础的字符编码,所以,需要先去将dataJsonStr转换为Unicode,然后再调用json.loads,就可以了。

代码如下:

dataJsonStrUni = dataJsonStr.decode("GB2312"); 
dataDict = json.loads(dataJsonStrUni, encoding="GB2312");

 

此处对应着上面解释中的:

Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to unicode first.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值