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