ujson的安装及示例
1,239 views
0
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类型
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2014-06-10 21:01:06
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: ujson的安装及示例 (米扑博客)