4
我不明白为什么以下类型从str改为unicode。为什么json.loads返回一个unicode对象而不是字符串
CASE1
Python 2.7 (r27:82500, Nov 19 2014, 18:07:42)
[GCC 4.5.1 20100924 (Red Hat 4.5.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> x = {'resources': {}, 'tags': ['a', 'b']}
>>> ret = json.dumps(x)
>>> ret
'{"resources": {}, "tags": ["a", "b"]}'
>>>
>>> type(ret)
<type 'str'>
>>> ret2 = json.loads(ret)
>>> ret2
{'resources': {}, 'tags': ['a', 'b']}
CASE2
Python 2.7.5 (default, Apr 22 2015, 21:27:15)
[GCC 4.9.2 20141101 (Red Hat 4.9.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> x = {'resources': {}, 'tags': ['a', 'b']}
>>> ret = json.dumps(x)
>>> ret
'{"resources": {}, "tags": ["a", "b"]}'
>>> type(x)
<type 'dict'>
>>> type(ret)
<type 'str'>
>>> ret2 = json.loads(ret)
>>> ret2
{u'resources': {}, u'tags': [u'a', u'b']}
>>>
因此,在2的情况下,我们看到在1的情况下,我们看到的字符串,而不是Unicode对象。 我看不到任何代码更改发生在可导致此问题的两个版本的python中。可能是我错过了一些东西。 任何线索将不胜感激。 谢谢
其实,我的意思是,在json.loads()函数定义中没有看到任何相关的代码更改。可能是我错过了一些东西。 – user3872776