ujson 安装

easy_install ujson      或        pip install ujson

 

ujson 示例

homer@ubuntu:~$ python
Python 2.7.6 (default, Jan 25 2014, 12:41:48) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ujson
>>> ujson
<module 'ujson' from '/home/homer/.python-eggs/ujson-1.33-py2.7-linux-x86_64.egg-tmp/ujson.so'>
>>> dir(ujson)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', 'decode', 'dump', 'dumps', 'encode', 'load', 'loads']
>>> ujson.dump(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes exactly 2 arguments (1 given)
>>> ujson.dump("1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes exactly 2 arguments (1 given)
>>> ujson.dumps(1)
'1'
>>> ujson.dump({1:2})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes exactly 2 arguments (1 given)
>>> ujson.dumps({1:2})
'{"1":2}'
>>> ujson.dumps({1:2,2:'a'})
'{"1":2,"2":"a"}'
>>> w=ujson.dumps({1:2,2:'a'})
>>> w
'{"1":2,"2":"a"}'
>>> s=ujson.loads(w)
>>> s
{u'1': 2, u'2': u'a'}
>>> s.keys()
[u'1', u'2']
>>> s[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> s["1"]
2

使用json时需要注意的地方:python中字典的key在经过json转化后都变成了string类型