Why json.loads returns a unicode object instead of a string

I cannot understand why the next type is changing to unicode from str.

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']}

Case 2

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']}
>>> 

So, in case 2, we see unicode objects, where, as in case 1, instead we see a line. I do not see any code changes in the two versions of python that could lead to this. Maybe I missed something. Any findings would be appreciated. Thanks

+4
source share
1 answer

2.7 Python , . 2.7.5. . 10038. , 2.6.6 2.7.5, , 2.7 .

, python , .

"", , ! Python , , . "json" Python 2.7.5 . , JSON Python 2.7.1, 2.7.2, 2.7.3 2.7.4.

+6

All Articles