由于项目需要使用python解析json数据,因此到网上找了一些python解析json模块方面的资料。
根据查找到的资料发现,人们使用的最多的json解析库是simplejson,json及cjson,而且按照网上的说法cjson的效率最好。
于是我就安装了cjson进行测试
>>> import cjson
>>> dir(cjson)
['DecodeError', 'EncodeError', 'Error', '__doc__', '__file__', '__name__', '__version__', 'decode', 'encode']
>>> w={1:2,3:4,'a':5}
>>> cjson.encode(w)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
cjson.EncodeError: JSON encodable dictionaries must have string/unicode keys
>>>
但是在测试过程中出现了错误,cjson模块只支持string/unicode的key,在网上找了一圈也没有找到解决办法。
随后在一篇博客中发现了ujson,号称是python中最好的json解析模块,详情请见博客: UltraJSON——Python 的极速 JSON 编解码器
ujson测试结果如下:
[root@portal-ol-213 python-cjson-1.0.6]# easy_install ujson
Searching for ujson
Reading http://pypi.python.org/simple/ujson/
Reading https://github.com/jskorpan/ultrajson
Reading http://www.esn.me
Reading https://github.com/esnme/ultrajson
Reading http://github.com/esnme/ultrajson
Best match: ujson 1.18
Downloading http://pypi.python.org/packages/source/u/ujson/ujson-1.18.zip#md5=8d033858770ff2222a001ee1fcd0ee15
Processing ujson-1.18.zip
Running ujson-1.18/setup.py -q bdist_egg --dist-dir /tmp/easy_install-6XioO2/ujson-1.18/egg-dist-tmp-NfNCU3
In file included from ./python/objToJSON.c:4:
./lib/ultrajson.h:298:7: warning: no newline at end of file
./python/objToJSON.c: In function ‘PyLongToINT64’:
./python/objToJSON.c:101: warning: unused variable ‘obj’
./python/objToJSON.c: In function ‘Dict_iterNext’:
./python/objToJSON.c:369: warning: passing argument 2 of ‘PyDict_Next’ from incompatible pointer type
./python/objToJSON.c: In function ‘objToJSON’:
./python/objToJSON.c:695: warning: initialization from incompatible pointer type
./python/objToJSON.c:706: warning: initialization from incompatible pointer type
./python/objToJSON.c: At top level:
./python/objToJSON.c:86: warning: ‘PyIntToINT32’ defined but not used
In file included from ./python/JSONtoObj.c:2:
./lib/ultrajson.h:298:7: warning: no newline at end of file
In file included from ./lib/ultrajsonenc.c:37:
./lib/ultrajson.h:298:7: warning: no newline at end of file
./lib/ultrajsonenc.c:121: warning: ‘fastcall’ attribute ignored
./lib/ultrajsonenc.c: In function ‘Buffer_EscapeStringUnvalidated’:
./lib/ultrajsonenc.c:199: warning: value computed is not used
./lib/ultrajsonenc.c: At top level:
./lib/ultrajsonenc.c:414: warning: ‘fastcall’ attribute ignored
./lib/ultrajsonenc.c:857:2: warning: no newline at end of file
./lib/ultrajsonenc.c: In function ‘Buffer_EscapeStringValidated’:
./lib/ultrajsonenc.c:216: warning: ‘ucs’ may be used uninitialized in this function
In file included from ./lib/ultrajsondec.c:37:
./lib/ultrajson.h:298:7: warning: no newline at end of file
./lib/ultrajsondec.c:55: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c:75: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c:283: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c:301: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c:323: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c:341: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c:391: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c: In function ‘decode_string’:
./lib/ultrajsondec.c:424: warning: pointer targets in assignment differ in signedness
./lib/ultrajsondec.c: At top level:
./lib/ultrajsondec.c:620: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c: In function ‘decode_array’:
./lib/ultrajsondec.c:633: warning: value computed is not used
./lib/ultrajsondec.c: At top level:
./lib/ultrajsondec.c:670: warning: ‘fastcall’ attribute ignored
./lib/ultrajsondec.c:746: warning: ‘fastcall’ attribute ignored
zip_safe flag not set; analyzing archive contents...
Adding ujson 1.18 to easy-install.pth file
Installed /usr/lib/python2.4/site-packages/ujson-1.18-py2.4-linux-x86_64.egg
Processing dependencies for ujson
Finished processing dependencies for ujson
[root@portal-ol-213 python-cjson-1.0.6]#
[root@portal-ol-213 python-cjson-1.0.6]#
[root@portal-ol-213 python-cjson-1.0.6]# python
Python 2.4.3 (#1, Sep 21 2011, 19:55:41)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ujson
>>> dir(ujson)
['__builtins__', '__doc__', '__file__', '__name__', '__version__', 'decode', 'dump', 'dumps', 'encode', 'load', 'loads']
>>> ujson.dump(1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function takes exactly 2 arguments (1 given)
>>> ujson.dumps(1)
'1'
>>> ujson.dumps({1:2})
'{"1":2}'
>>>
>>> ujson.dumps({1:2,2:'a'})
'{"1":2,"2":"a"}'
>>> w=ujson.dumps({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 ?
KeyError: 1
>>> s['1']
2
>>>
使用json时需要注意的地方:python中字典的key在经过json转化后都变成了string类型